Skip to content

Commit

Permalink
Merge pull request #18 from intuit/ssr
Browse files Browse the repository at this point in the history
make it ssr friendly
  • Loading branch information
tylerkrupicka committed Apr 25, 2021
2 parents e4e49d7 + 57142c5 commit b349f62
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
4 changes: 0 additions & 4 deletions .circleci/config.yml
Expand Up @@ -165,7 +165,3 @@ workflows:
requires:
- lint
- unit-tests
filters:
branches:
only:
- master
2 changes: 1 addition & 1 deletion components/Table/src/index.tsx
Expand Up @@ -114,7 +114,7 @@ export const HeadCell = (props: Element<"th">) => {

/** Enable resizing and set defaults when click starts */
const mouseDownHandler = (e: React.MouseEvent) => {
if (!window || !headRef.current) return;
if (typeof window === "undefined" || !headRef.current) return;
const s = window.getComputedStyle(headRef.current);
setWidth(parseInt(s.width, 10));
setStartingX(e.clientX);
Expand Down
10 changes: 9 additions & 1 deletion packages/storybook-theme-addon/src/theme/theme.tsx
Expand Up @@ -13,6 +13,8 @@ import FireFox from "./icons/firefox";

import { THEME_SELECT_TOOL_ID, PARAMETER_NAME } from "../constants";

const isWindowDefined = typeof window !== "undefined";

interface TooltipProps {
/** Called when the tooltip hides */
onHide: () => void;
Expand All @@ -38,12 +40,18 @@ const STORAGE_TOKEN = `${THEME_SELECT_TOOL_ID}-storage`;

/** Persist the theme settings in localStorage */
const saveLocalStorage = (data: State) => {
if (!isWindowDefined) {
return;
}

window.localStorage.setItem(STORAGE_TOKEN, JSON.stringify(data));
};

/** Restore theme settings from localStorage */
const restoreLocalStorage = (defaultTheme?: Partial<State>): State => {
const data = window.localStorage.getItem(STORAGE_TOKEN);
const data = isWindowDefined
? window.localStorage.getItem(STORAGE_TOKEN)
: undefined;

if (data) {
const storedTheme = JSON.parse(data) as State;
Expand Down
12 changes: 8 additions & 4 deletions packages/themes/src/AutoThemeProvider.tsx
Expand Up @@ -2,9 +2,11 @@ import React from "react";
import { all } from "./themes";
import { ThemeableElement, ThemeContext } from "./utils";

const isWindowDefined = typeof window !== "undefined";

/** Determine if the current browser is FireFox */
const isFirefox = () => {
if (window?.navigator?.userAgent) {
export const isFirefox = () => {
if (isWindowDefined && window?.navigator?.userAgent) {
if (window.navigator.userAgent.toLowerCase().includes("firefox")) {
return true;
}
Expand All @@ -26,11 +28,13 @@ export interface AutoThemeProviderProps extends ThemeableElement<"div"> {
*/
const useDarkMode = () => {
const [darkMode, setDarkMode] = React.useState(
window ? window.matchMedia("(prefers-color-scheme: dark)").matches : false
isWindowDefined && window
? window.matchMedia("(prefers-color-scheme: dark)").matches
: false
);

React.useEffect(() => {
if (!window) {
if (!isWindowDefined) {
return;
}

Expand Down
8 changes: 6 additions & 2 deletions packages/themes/src/utils.tsx
Expand Up @@ -8,6 +8,8 @@ export type ColorScheme = typeof colorSchemes[number];
export const themes = ["chrome", "firefox"] as const;
export type Theme = typeof themes[number];

const isWindowDefined = typeof window !== "undefined";

/**
* Get all of the props for an HTML element + add the theme props.
* Used to easily type the rest props of a component and add theming.
Expand Down Expand Up @@ -40,11 +42,13 @@ export const ThemeContext = React.createContext<Themeable>({
*/
const useDarkMode = () => {
const [darkMode, setDarkMode] = React.useState(
window ? window.matchMedia("(prefers-color-scheme: dark)").matches : false
isWindowDefined && window
? window.matchMedia("(prefers-color-scheme: dark)").matches
: false
);

React.useEffect(() => {
if (!window) {
if (!isWindowDefined) {
return;
}

Expand Down

0 comments on commit b349f62

Please sign in to comment.