Skip to content

Commit

Permalink
add dark mode support
Browse files Browse the repository at this point in the history
  • Loading branch information
mnajdova committed Dec 4, 2024
1 parent d7ebf06 commit a768502
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 130 deletions.
296 changes: 172 additions & 124 deletions examples/pigment-css-vite-ts/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { styled, css, keyframes } from '@pigment-css/react';
import { ColorSchemeProvider, useColorScheme } from './components/ColorSchemeProvider';

const scale = keyframes({
to: { scale: 'var(--s2)' },
Expand Down Expand Up @@ -58,139 +59,186 @@ function generateBubbleVars() {
`;
}

export default function Home() {
const IconButton = styled('button')({
background: 'transparent',
border: 'none',
cursor: 'pointer',
padding: '0',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
});

function ColorSchemeToggleButton() {
const { colorScheme, setColorScheme } = useColorScheme();

const toggleColorScheme = () => {
setColorScheme(colorScheme === 'dark' ? 'light' : 'dark');
};

return (
<main
className={css({
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
height: '100lvh',
padding: '20px',
color: 'hsl(var(--palette-foreground))',
backgroundColor: 'hsl(var(--palette-background))',
fontFamily:
"system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'",
})}
>
<h1
className={`my-custom-class ${css(({ theme }) => ({
fontFamily: 'system-ui, sans-serif',
fontSize: '4rem',
fontWeight: 500,
textAlign: 'center',
position: 'relative',
display: 'flex',
alignItems: 'center',
color: '#888',
marginBottom: '1rem',
...theme.applyStyles('dark', { color: '#fff' }),
}))}`}
>
Pigment&nbsp;CSS
<span
className={css(({ theme }) => ({
position: 'absolute',
inset: '0',
background: 'white',
mixBlendMode: 'color-burn',
overflow: 'hidden',
pointerEvents: 'none',
...theme.applyStyles('dark', {
mixBlendMode: 'darken',
filter: 'brightness(2)',
}),
}))}
>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
</span>
</h1>
<div
className={css({
fontFamily: 'system-ui, sans-serif',
letterSpacing: '2px',
opacity: 0.6,
lineHeight: 2,
textAlign: 'center',
textWrap: 'balance',
})}
>
CSS-in-JS library with static extraction
</div>
<div sx={{ position: 'absolute', top: 10, right: 10 }}>
<IconButton sx={{ fontSize: 20, px: 1.5 }} onClick={toggleColorScheme}>
{colorScheme === 'light' ? '🌙' : '🔆'}
</IconButton>
</div>
);
}

function getCookie(name: string) {
return document.cookie
.split('; ')
.find((row) => row.startsWith(`${name}=`))
?.split('=')[1];
}

function Home() {
const { colorScheme } = useColorScheme();

return (
<main className={colorScheme}>
<div
className={css({
display: 'flex',
flexWrap: 'wrap',
gap: '1rem',
marginTop: '2rem',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
height: '100lvh',
padding: '20px',
color: 'hsl(var(--palette-foreground))',
backgroundColor: 'hsl(var(--palette-background))',
fontFamily:
"system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'",
})}
>
<Link
href="https://github.com/mui/pigment-css/blob/master/README.md"
target="_blank"
rel="noopener noreferrer"
<ColorSchemeToggleButton />
<h1
className={`my-custom-class ${css(({ theme }) => ({
fontFamily: 'system-ui, sans-serif',
fontSize: '4rem',
fontWeight: 500,
textAlign: 'center',
position: 'relative',
display: 'flex',
alignItems: 'center',
color: '#888',
marginBottom: '1rem',
...theme.applyStyles('dark', { color: '#fff' }),
}))}`}
>
Pigment&nbsp;CSS
<span
className={css(({ theme }) => ({
position: 'absolute',
inset: '0',
background: 'white',
mixBlendMode: 'color-burn',
overflow: 'hidden',
pointerEvents: 'none',
...theme.applyStyles('dark', {
mixBlendMode: 'darken',
filter: 'brightness(2)',
}),
}))}
>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
<Bubble
className={css`
${generateBubbleVars()}
`}
/>
</span>
</h1>
<div
className={css({
fontFamily: 'system-ui, sans-serif',
letterSpacing: '2px',
opacity: 0.6,
lineHeight: 2,
textAlign: 'center',
textWrap: 'balance',
})}
>
Documentation
</Link>
<Link
outlined
href="https://github.com/orgs/mui/projects/27/views/3"
target="_blank"
rel="noopener noreferrer"
CSS-in-JS library with static extraction
</div>
<div
className={css({
display: 'flex',
flexWrap: 'wrap',
gap: '1rem',
marginTop: '2rem',
})}
>
Roadmap
</Link>
<Link
href="https://github.com/mui/pigment-css/blob/master/README.md"
target="_blank"
rel="noopener noreferrer"
>
Documentation
</Link>
<Link
outlined
href="https://github.com/orgs/mui/projects/27/views/3"
target="_blank"
rel="noopener noreferrer"
>
Roadmap
</Link>
</div>
</div>
</main>
);
}
export default function App() {
const colorScheme = getCookie('colorScheme') || 'light';

return (
<ColorSchemeProvider colorScheme={colorScheme}>
<Home />
</ColorSchemeProvider>
);
}
11 changes: 11 additions & 0 deletions examples/pigment-css-vite-ts/src/augment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,14 @@ declare module '@pigment-css/react/theme' {
}>;
}
}

declare global {
namespace React {
interface HTMLAttributes {
sx?: SxProps<Theme>;
}
interface SVGProps {
sx?: SxProps<Theme>;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import * as React from 'react';

const ColorSchemeContext = React.createContext<{
colorScheme: string;
setColorScheme: React.Dispatch<React.SetStateAction<string>>;
}>({
colorScheme: 'light',
setColorScheme: () => '',
});

function setCookie(name: string, value: string, days: number = 100) {
let expires = '';
if (days) {
const date = new Date();
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
expires = `; expires=${date.toUTCString()}`;
}
document.cookie = `${name}=${value || ''}${expires}; path=/`;
}

export function ColorSchemeProvider({
colorScheme: initialColorScheme,
children,
}: React.PropsWithChildren<{ colorScheme: string }>) {
const [colorScheme, setColorScheme] = React.useState<string>(initialColorScheme);

const contextValue = React.useMemo(
() => ({ colorScheme, setColorScheme }),
[colorScheme, setColorScheme],
);

// Set the colorScheme in localStorage
React.useEffect(() => {
setCookie('colorScheme', colorScheme);
localStorage.setItem('colorScheme', colorScheme);
}, [colorScheme]);

// Handle when localStorage has changed
React.useEffect(() => {
const handleStorage = (event: StorageEvent) => {
const value = event.newValue;
if (
typeof event.key === 'string' &&
event.key === 'colorScheme' &&
typeof value === 'string'
) {
setColorScheme(value);
}
};
// For syncing color-scheme changes between iframes
window.addEventListener('storage', handleStorage);
return () => {
window.removeEventListener('storage', handleStorage);
};
}, [setColorScheme]);

return <ColorSchemeContext.Provider value={contextValue}>{children}</ColorSchemeContext.Provider>;
}

export const useColorScheme = () => {
return React.useContext(ColorSchemeContext);
};
Loading

0 comments on commit a768502

Please sign in to comment.