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(component): Check for window in components #124

Merged
merged 3 commits into from
May 17, 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
3 changes: 2 additions & 1 deletion src/lib/components/Carousel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import classNames from 'classnames';
import { HiOutlineChevronLeft, HiOutlineChevronRight } from 'react-icons/hi';
import ScrollContainer from 'react-indiana-drag-scroll';
import windowExists from '../../helpers/window-exists';

export type CarouselProps = PropsWithChildren<{
slide?: boolean;
Expand All @@ -35,7 +36,7 @@ export const Carousel: FC<CarouselProps> = ({
const [activeItem, setActiveItem] = useState(0);
const [isDragging, setIsDragging] = useState(false);
const carouselContainer = useRef<HTMLDivElement>(null);
const isDeviceMobile = typeof window.orientation !== 'undefined' || navigator.userAgent.indexOf('IEMobile') !== -1;
const isDeviceMobile = windowExists() && navigator.userAgent.indexOf('IEMobile') !== -1;

const items = useMemo(
() =>
Expand Down
21 changes: 17 additions & 4 deletions src/lib/components/Flowbite/ThemeContext.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable react-hooks/rules-of-hooks */
import { FC, ReactNode, createContext, useContext, useState, useEffect } from 'react';
import windowExists from '../../helpers/window-exists';
import defaultTheme from '../../theme/default';

export type Mode = string | undefined | 'light' | 'dark';
Expand Down Expand Up @@ -36,16 +37,22 @@ export const useThemeMode = (
const savePreference = (m: string) => localStorage.setItem('theme', m);

const toggleMode = () => {
if (!mode) return;
if (!mode) {
return;
}

if (windowExists()) {
document.documentElement.classList.toggle('dark');
}

document.documentElement.classList.toggle('dark');
savePreference(mode);
setMode(mode == 'dark' ? 'light' : 'dark');
};

if (usePreferences) {
useEffect(() => {
const userPreference = !!window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
const userPreference =
windowExists() && !!window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
const userMode = localStorage.getItem('theme') || (userPreference ? 'dark' : 'light');

if (userMode) {
Expand All @@ -54,10 +61,16 @@ export const useThemeMode = (
}, []);

useEffect(() => {
if (!mode) return;
if (!mode) {
return;
}

savePreference(mode);

if (!windowExists()) {
return;
}

if (mode != 'dark') {
document.documentElement.classList.remove('dark');
} else {
Expand Down
6 changes: 5 additions & 1 deletion src/lib/components/Flowbite/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FC, HTMLAttributes, useLayoutEffect, useMemo } from 'react';
import { ThemeContext, useThemeMode } from './ThemeContext';
import { mergeDeep } from '../../helpers/mergeDeep';
import defaultTheme from '../../theme/default';
import windowExists from '../../helpers/window-exists';

export interface ThemeProps {
config?: object;
Expand All @@ -25,7 +26,10 @@ export const Flowbite: FC<FlowbiteProps> = ({ children, theme = {} }) => {
if (setMode != null) {
setMode('dark');
}
document.documentElement.classList.add('dark');

if (windowExists()) {
document.documentElement.classList.add('dark');
}
}
}, [dark, setMode]);

Expand Down
53 changes: 29 additions & 24 deletions src/lib/components/Modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ModalHeader } from './ModalHeader';
import { ModalBody } from './ModalBody';
import { ModalFooter } from './ModalFooter';
import { ModalContext } from './ModalContext';
import windowExists from '../../helpers/window-exists';

type Size = 'sm' | 'md' | 'lg' | 'xl' | '2xl' | '3xl' | '4xl' | '5xl' | '6xl' | '7xl';
type Placement = `${'top' | 'bottom'}-${'left' | 'center' | 'right'}` | `center${'' | '-left' | '-right'}`;
Expand Down Expand Up @@ -46,17 +47,19 @@ const placementClasses: Record<Placement, string> = {

const ModalComponent: FC<ModalProps> = ({
children,
root = document.body,
root = windowExists() ? document.body : undefined,
show,
popup,
size = '2xl',
placement = 'center',
onClose,
}): JSX.Element | null => {
const [container] = useState<HTMLDivElement>(document.createElement('div'));
const [container] = useState<HTMLDivElement | undefined>(windowExists() ? document.createElement('div') : undefined);

useEffect(() => {
if (!show) return;
if (!container || !root || !show) {
return;
}

root.appendChild(container);

Expand All @@ -65,27 +68,29 @@ const ModalComponent: FC<ModalProps> = ({
};
}, [container, root, show]);

return createPortal(
<ModalContext.Provider value={{ popup, onClose }}>
<div
aria-hidden={!show}
className={classNames(
'fixed top-0 right-0 left-0 z-50 h-modal overflow-y-auto overflow-x-hidden md:inset-0 md:h-full',
placementClasses[placement],
{
'flex bg-gray-900 bg-opacity-50 dark:bg-opacity-80': show,
hidden: !show,
},
)}
data-testid="modal"
>
<div className={classNames('relative h-full w-full p-4 md:h-auto', sizeClasses[size])}>
<div className="relative rounded-lg bg-white shadow dark:bg-gray-700">{children}</div>
</div>
</div>
</ModalContext.Provider>,
container,
);
return container
? createPortal(
<ModalContext.Provider value={{ popup, onClose }}>
<div
aria-hidden={!show}
className={classNames(
'fixed top-0 right-0 left-0 z-50 h-modal overflow-y-auto overflow-x-hidden md:inset-0 md:h-full',
placementClasses[placement],
{
'flex bg-gray-900 bg-opacity-50 dark:bg-opacity-80': show,
hidden: !show,
},
)}
data-testid="modal"
>
<div className={classNames('relative h-full w-full p-4 md:h-auto', sizeClasses[size])}>
<div className="relative rounded-lg bg-white shadow dark:bg-gray-700">{children}</div>
</div>
</div>
</ModalContext.Provider>,
container,
)
: null;
};

ModalComponent.displayName = 'Modal';
Expand Down
1 change: 1 addition & 0 deletions src/lib/helpers/window-exists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default (): boolean => typeof window !== 'undefined';