Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ssr #215

Merged
merged 3 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/weak-seals-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cube-dev/ui-kit": patch
---

Fix SSR support
8 changes: 5 additions & 3 deletions src/components/Root.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useEffect, useRef, useState } from 'react';
import { ModalProvider } from '@react-aria/overlays';
import { StyleSheetManager } from 'styled-components';
import { useViewportSize } from '@react-aria/utils';

import {
BASE_STYLES,
Expand All @@ -13,6 +12,7 @@ import {
} from '../tasty';
import { Provider } from '../provider';
import { TOKENS } from '../tokens';
import { useViewportSize } from '../utils/react';

import { PortalProvider } from './portal';
import { GlobalStyles } from './GlobalStyles';
Expand Down Expand Up @@ -47,7 +47,9 @@ export interface CubeRootProps extends BaseProps {
}

const IS_DVH_SUPPORTED =
typeof CSS?.supports !== 'undefined' ? CSS.supports('height: 100dvh') : false;
typeof CSS !== 'undefined' && typeof CSS?.supports === 'function'
? CSS.supports('height: 100dvh')
: false;

export function Root(allProps: CubeRootProps) {
let {
Expand Down Expand Up @@ -81,7 +83,7 @@ export function Root(allProps: CubeRootProps) {
let timeoutRef = useRef<NodeJS.Timeout>();

useEffect(() => {
if (IS_DVH_SUPPORTED) {
if (IS_DVH_SUPPORTED && typeof window !== 'undefined') {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/tasty/styles/dimension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const DEFAULT_MIN_SIZE = 'var(--gap)';
const DEFAULT_MAX_SIZE = '100%';

function isSizingSupport(val) {
return typeof CSS !== 'undefined' && typeof CSS.supports !== 'undefined'
return typeof CSS !== 'undefined' && typeof CSS?.supports === 'function'
? CSS.supports('height', val)
: false;
}
Expand Down
4 changes: 3 additions & 1 deletion src/tasty/utils/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ export type StyleStateListMap = { [key: string]: StyleStateList };
const devMode = process.env.NODE_ENV !== 'production';

const IS_DVH_SUPPORTED =
typeof CSS?.supports !== 'undefined' ? CSS.supports('height: 100dvh') : false;
typeof CSS !== 'undefined' && typeof CSS?.supports === 'function'
? CSS.supports('height: 100dvh')
: false;

export const CUSTOM_UNITS = {
r: 'var(--radius)',
Expand Down
1 change: 1 addition & 0 deletions src/utils/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export { useSlotProps, SlotProvider, ClearSlots } from './Slots';
export { useLayoutEffect } from './useLayoutEffect';
export { useCombinedRefs } from './useCombinedRefs';
export { wrapNodeIfPlain } from './wrapNodeIfPlain';
export { useViewportSize } from './useViewportSize';
53 changes: 53 additions & 0 deletions src/utils/react/useViewportSize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { useEffect, useState } from 'react';

interface ViewportSize {
width: number;
height: number;
}

// @ts-ignore
let visualViewport = typeof window !== 'undefined' && window.visualViewport;

export function useViewportSize(): ViewportSize {
let [size, setSize] = useState(() => getViewportSize());
tenphi marked this conversation as resolved.
Show resolved Hide resolved

useEffect(() => {
// Use visualViewport api to track available height even on iOS virtual keyboard opening
let onResize = () => {
tenphi marked this conversation as resolved.
Show resolved Hide resolved
setSize((size) => {
let newSize = getViewportSize();
if (newSize.width === size.width && newSize.height === size.height) {
return size;
}
return newSize;
});
};

if (!visualViewport) {
window.addEventListener('resize', onResize);
} else {
visualViewport.addEventListener('resize', onResize);
}

return () => {
if (!visualViewport) {
window.removeEventListener('resize', onResize);
} else {
visualViewport.removeEventListener('resize', onResize);
}
};
}, []);

return size;
}

function getViewportSize(): ViewportSize {
return {
width:
(visualViewport && visualViewport.width) ||
(typeof window !== 'undefined' ? window.innerWidth : 0),
height:
(visualViewport && visualViewport.height) ||
(typeof window !== 'undefined' ? window.innerHeight : 0),
};
}