-
Notifications
You must be signed in to change notification settings - Fork 13
feat(theme): scoped theming with ThemeScope component #768
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
Open
paanSinghCoder
wants to merge
1
commit into
main
Choose a base branch
from
feat/scoped-theming
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| 'use client'; | ||
|
|
||
| import { | ||
| Button, | ||
| Callout, | ||
| Flex, | ||
| IconButton, | ||
| Text, | ||
| ThemeScope | ||
| } from '@raystack/apsara'; | ||
| import { Moon, Sun } from 'lucide-react'; | ||
| import { useState } from 'react'; | ||
| import { useTheme } from '@/components/theme'; | ||
|
|
||
| type ScopeTheme = 'light' | 'dark'; | ||
|
|
||
| const panelStyle = { | ||
| minWidth: 320, | ||
| padding: 'var(--rs-space-7)', | ||
| borderRadius: 'var(--rs-space-3)', | ||
| border: '1px solid var(--rs-color-border-base-primary)', | ||
| backgroundColor: 'var(--rs-color-background-base-primary)' | ||
| }; | ||
|
|
||
| const Page = () => { | ||
| const { theme, setTheme } = useTheme(); | ||
| const [scopeTheme, setScopeTheme] = useState<ScopeTheme>('dark'); | ||
| const [calloutScopeTheme, setCalloutScopeTheme] = | ||
| useState<ScopeTheme>('light'); | ||
| const GlobalIcon = theme === 'dark' ? Sun : Moon; | ||
|
|
||
| return ( | ||
| <Flex | ||
| direction='column' | ||
| gap={9} | ||
| style={{ | ||
| minHeight: '100vh', | ||
| padding: 'var(--rs-space-11)', | ||
| backgroundColor: 'var(--rs-color-background-base-primary)' | ||
| }} | ||
| > | ||
| <Flex justify='between' align='center'> | ||
| <Text size={6} weight={500}> | ||
| Scoped Theming | ||
| </Text> | ||
| <IconButton | ||
| aria-label='Toggle page theme' | ||
| size={3} | ||
| onClick={() => | ||
| setTheme({ theme: theme === 'dark' ? 'light' : 'dark' }) | ||
| } | ||
| > | ||
| <GlobalIcon /> | ||
| </IconButton> | ||
| </Flex> | ||
|
|
||
| <ThemeScope | ||
| theme={scopeTheme} | ||
| render={ | ||
| <Flex | ||
| direction='column' | ||
| gap={5} | ||
| style={{ ...panelStyle, alignSelf: 'flex-start' }} | ||
| /> | ||
| } | ||
| > | ||
| <Flex justify='between' align='center' gap={5}> | ||
| <Text size={4} weight={500}> | ||
| Scoped box | ||
| </Text> | ||
| <IconButton | ||
| aria-label='Toggle scope theme' | ||
| size={3} | ||
| onClick={() => | ||
| setScopeTheme(scopeTheme === 'dark' ? 'light' : 'dark') | ||
| } | ||
| > | ||
| {scopeTheme === 'dark' ? <Sun /> : <Moon />} | ||
| </IconButton> | ||
| </Flex> | ||
| <Text size={3} variant='secondary'> | ||
| This box themes itself via{' '} | ||
| <code>data-theme="{scopeTheme}"</code>, independent of the | ||
| page. | ||
| </Text> | ||
| <Flex gap={3}> | ||
| <Button variant='solid' color='accent'> | ||
| Solid | ||
| </Button> | ||
| <Button variant='outline' color='neutral'> | ||
| Outline | ||
| </Button> | ||
| </Flex> | ||
| </ThemeScope> | ||
|
|
||
| <ThemeScope | ||
| theme={calloutScopeTheme} | ||
| render={ | ||
| <Flex | ||
| direction='column' | ||
| gap={4} | ||
| style={{ ...panelStyle, alignSelf: 'flex-start' }} | ||
| /> | ||
| } | ||
| > | ||
| <Flex justify='between' align='center' gap={5}> | ||
| <Text size={4} weight={500}> | ||
| Semantic colors in scope | ||
| </Text> | ||
| <IconButton | ||
| aria-label='Toggle callout scope theme' | ||
| size={3} | ||
| onClick={() => | ||
| setCalloutScopeTheme( | ||
| calloutScopeTheme === 'dark' ? 'light' : 'dark' | ||
| ) | ||
| } | ||
| > | ||
| {calloutScopeTheme === 'dark' ? <Sun /> : <Moon />} | ||
| </IconButton> | ||
| </Flex> | ||
| <Text size={3} variant='secondary'> | ||
| Accent, success, danger, and attention tokens all re-resolve at the | ||
| scope. | ||
| </Text> | ||
| <Callout type='accent'>Accent — informational message</Callout> | ||
| <Callout type='success'>Success — operation completed</Callout> | ||
| <Callout type='alert'>Danger — something went wrong</Callout> | ||
| <Callout type='attention'>Attention — review before continuing</Callout> | ||
| </ThemeScope> | ||
| </Flex> | ||
| ); | ||
| }; | ||
|
|
||
| export default Page; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
packages/raystack/components/theme-provider/__tests__/theme-scope.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { ThemeScope } from '../theme-scope'; | ||
|
|
||
| describe('ThemeScope', () => { | ||
| describe('Default rendering', () => { | ||
| it('renders a div by default with the provided children', () => { | ||
| render( | ||
| <ThemeScope theme='dark' data-testid='scope'> | ||
| <span>inside</span> | ||
| </ThemeScope> | ||
| ); | ||
|
|
||
| const node = screen.getByTestId('scope'); | ||
| expect(node.tagName).toBe('DIV'); | ||
| expect(screen.getByText('inside')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('writes data-theme when theme is provided', () => { | ||
| render(<ThemeScope theme='dark' data-testid='scope' />); | ||
| expect(screen.getByTestId('scope')).toHaveAttribute('data-theme', 'dark'); | ||
| }); | ||
|
|
||
| it('omits data attributes when their props are not provided', () => { | ||
| render(<ThemeScope data-testid='scope' />); | ||
|
|
||
| const node = screen.getByTestId('scope'); | ||
| expect(node).not.toHaveAttribute('data-theme'); | ||
| expect(node).not.toHaveAttribute('data-accent-color'); | ||
| expect(node).not.toHaveAttribute('data-gray-color'); | ||
| expect(node).not.toHaveAttribute('data-style'); | ||
| }); | ||
|
|
||
| it('writes every supported data attribute', () => { | ||
| render( | ||
| <ThemeScope | ||
| theme='light' | ||
| accentColor='orange' | ||
| grayColor='mauve' | ||
| styleVariant='traditional' | ||
| data-testid='scope' | ||
| /> | ||
| ); | ||
|
|
||
| const node = screen.getByTestId('scope'); | ||
| expect(node).toHaveAttribute('data-theme', 'light'); | ||
| expect(node).toHaveAttribute('data-accent-color', 'orange'); | ||
| expect(node).toHaveAttribute('data-gray-color', 'mauve'); | ||
| expect(node).toHaveAttribute('data-style', 'traditional'); | ||
| }); | ||
|
|
||
| it('forwards arbitrary HTML attributes', () => { | ||
| render( | ||
| <ThemeScope | ||
| theme='dark' | ||
| id='my-scope' | ||
| className='custom-class' | ||
| data-testid='scope' | ||
| /> | ||
| ); | ||
|
|
||
| const node = screen.getByTestId('scope'); | ||
| expect(node).toHaveAttribute('id', 'my-scope'); | ||
| expect(node).toHaveClass('custom-class'); | ||
| }); | ||
|
|
||
| it('passes through user-provided style', () => { | ||
| render( | ||
| <ThemeScope | ||
| theme='dark' | ||
| style={{ background: 'red' }} | ||
| data-testid='scope' | ||
| /> | ||
| ); | ||
|
|
||
| expect(screen.getByTestId('scope')).toHaveStyle({ background: 'red' }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('render prop', () => { | ||
| it('renders the provided element instead of a default div', () => { | ||
| render( | ||
| <ThemeScope theme='dark' render={<section data-testid='scope' />}> | ||
| <span>inside</span> | ||
| </ThemeScope> | ||
| ); | ||
|
|
||
| const node = screen.getByTestId('scope'); | ||
| expect(node.tagName).toBe('SECTION'); | ||
| expect(screen.getByText('inside')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('merges data attributes onto the rendered element', () => { | ||
| render( | ||
| <ThemeScope | ||
| theme='dark' | ||
| accentColor='mint' | ||
| render={<section data-testid='scope' />} | ||
| /> | ||
| ); | ||
|
|
||
| const node = screen.getByTestId('scope'); | ||
| expect(node).toHaveAttribute('data-theme', 'dark'); | ||
| expect(node).toHaveAttribute('data-accent-color', 'mint'); | ||
| }); | ||
|
|
||
| it('preserves the rendered element’s own attributes alongside the merged ones', () => { | ||
| render( | ||
| <ThemeScope | ||
| theme='light' | ||
| render={ | ||
| <section | ||
| id='my-section' | ||
| className='base-class' | ||
| data-testid='scope' | ||
| /> | ||
| } | ||
| /> | ||
| ); | ||
|
|
||
| const node = screen.getByTestId('scope'); | ||
| expect(node).toHaveAttribute('id', 'my-section'); | ||
| expect(node).toHaveClass('base-class'); | ||
| expect(node).toHaveAttribute('data-theme', 'light'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| export { ThemeSwitcher } from "./switcher"; | ||
| export { ThemeProvider, useTheme } from "./theme"; | ||
| export { ThemeProviderProps } from "./types"; | ||
| export { ThemeSwitcher } from './switcher'; | ||
| export { ThemeProvider, useTheme } from './theme'; | ||
| export { ThemeScope, type ThemeScopeProps } from './theme-scope'; | ||
| export { ThemeProviderProps } from './types'; | ||
| // Note: This themeProvider folder is a merge of old and the new themeProvider. Both old and the v1 folder contains the exact copy of themeProvider which was merged. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use a radius token for
borderRadiushere.borderRadiusis pulling from the spacing scale. That makes the example teach the wrong token family and can drift if spacing and radius scales stop lining up.Suggested fix
const panelStyle = { minWidth: 320, padding: 'var(--rs-space-7)', - borderRadius: 'var(--rs-space-3)', + borderRadius: 'var(--rs-radius-3)', border: '1px solid var(--rs-color-border-base-primary)', backgroundColor: 'var(--rs-color-background-base-primary)' };📝 Committable suggestion
🤖 Prompt for AI Agents