Skip to content

Commit

Permalink
Update styledsystemprovider to accept a ref (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexasselin008 authored Feb 21, 2024
2 parents 2a055f2 + 7717443 commit 53edc8a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-bobcats-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hopper-ui/styled-system": patch
---

StyledSystemProvider now accepts a ref
42 changes: 30 additions & 12 deletions packages/styled-system/src/StyledSystemProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import clsx from "clsx";
import { useCallback, useState, type ReactNode } from "react";
import { useCallback, useState, type ReactNode, forwardRef, type ForwardedRef } from "react";
import { ColorSchemeContext, type ColorScheme, type ColorSchemeContextType, type ColorSchemeOrSystem } from "./color-scheme/ColorSchemeContext.ts";
import { useColorScheme } from "./color-scheme/useColorScheme.ts";
import { BodyStyleProvider } from "./global-styles/BodyStyleProvider.tsx";
Expand All @@ -8,6 +8,8 @@ import { BreakpointProvider, DefaultUnsupportedMatchMediaBreakpoint, type Breakp
import { HopperRootCssClass, StyledSystemRootCssClass } from "./styled-system-root-css-class.ts";
import { TokenProvider } from "./tokens/TokenProvider.tsx";

const GlobalStyledSystemProviderCssSelector = "hop-StyledSystemProvider";

export interface StyledSystemProviderProps extends BreakpointProviderProps, DivProps {
/** The children of the component */
children: ReactNode;
Expand Down Expand Up @@ -35,13 +37,18 @@ export interface StyledSystemProviderProps extends BreakpointProviderProps, DivP
withCssVariables?: boolean;
}

/**
* StyledSystemProvider is required to be rendered at the root of your application. It is responsible for:
* - Adding CSS variables to the document
* - Managing color scheme (light, dark, auto)
* - Optionally adding body styles to the document
*/
export function StyledSystemProvider({ children, withBodyStyle = false, withCssVariables = true, colorScheme = "light", defaultColorScheme = "light", unsupportedMatchMediaBreakpoint = DefaultUnsupportedMatchMediaBreakpoint, className, ...rest }: StyledSystemProviderProps) {
const StyledSystemProvider = (props:StyledSystemProviderProps, ref: ForwardedRef<HTMLDivElement>) => {
const {
children,
withBodyStyle = false,
withCssVariables = true,
colorScheme = "light",
defaultColorScheme = "light",
unsupportedMatchMediaBreakpoint = DefaultUnsupportedMatchMediaBreakpoint,
className,
...rest
} = props;

const [remoteColorScheme, setRemoteColorScheme] = useState<ColorScheme>();
const computedColorScheme = useColorScheme(remoteColorScheme ?? colorScheme, defaultColorScheme);

Expand All @@ -50,11 +57,12 @@ export function StyledSystemProvider({ children, withBodyStyle = false, withCssV
}, [setRemoteColorScheme]);

const classNames = clsx(
className,
GlobalStyledSystemProviderCssSelector,
HopperRootCssClass,
`${HopperRootCssClass}-${computedColorScheme}`,
StyledSystemRootCssClass,
`${StyledSystemRootCssClass}-${computedColorScheme}`,
className
`${StyledSystemRootCssClass}-${computedColorScheme}`
);

return (
Expand All @@ -65,13 +73,23 @@ export function StyledSystemProvider({ children, withBodyStyle = false, withCssV
}}
>
<BreakpointProvider unsupportedMatchMediaBreakpoint={unsupportedMatchMediaBreakpoint}>
<Div className={classNames} {...rest}>
<Div ref={ref} className={classNames} {...rest}>
{withBodyStyle && <BodyStyleProvider />}
{withCssVariables && <TokenProvider />}
{children}
</Div>
</BreakpointProvider>
</ColorSchemeContext.Provider>
);
}
};

/**
* StyledSystemProvider is required to be rendered at the root of your application. It is responsible for:
* - Adding CSS variables to the document
* - Managing color scheme (light, dark, auto)
* - Optionally adding body styles to the document
*/
const _StyledSystemProvider = forwardRef<HTMLDivElement, StyledSystemProviderProps>(StyledSystemProvider);
_StyledSystemProvider.displayName = "StyledSystemProvider";

export { _StyledSystemProvider as StyledSystemProvider };

0 comments on commit 53edc8a

Please sign in to comment.