-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ThemeProvider): inner provider should overwrite specified props a…
…nd only them
- Loading branch information
Showing
13 changed files
with
169 additions
and
66 deletions.
There are no files selected for viewing
This file contains 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 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 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,70 @@ | ||
import React from 'react'; | ||
|
||
import type {Meta, StoryObj} from '@storybook/react'; | ||
|
||
import {Button} from '../../Button'; | ||
import {Text} from '../../Text'; | ||
import {ThemeProvider} from '../ThemeProvider'; | ||
import {useDirection} from '../useDirection'; | ||
|
||
const meta: Meta<typeof ThemeProvider> = { | ||
title: 'Components/Utils/ThemeProvider', | ||
component: ThemeProvider, | ||
tags: ['nodocs'], | ||
argTypes: { | ||
theme: { | ||
options: ['none', 'light', 'dark', 'light-hc', 'dark-hc', 'system'], | ||
control: { | ||
type: 'select', | ||
}, | ||
mapping: { | ||
none: undefined, | ||
}, | ||
}, | ||
direction: { | ||
options: ['none', 'ltr', 'rtl'], | ||
control: { | ||
type: 'radio', | ||
}, | ||
mapping: { | ||
none: undefined, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof ThemeProvider>; | ||
|
||
function ScopedComponent() { | ||
return ( | ||
<div style={{transform: 'scaleX(var(--g-flow-direction))'}}> | ||
<Button>{`current direction: ${useDirection()}`}</Button> | ||
</div> | ||
); | ||
} | ||
|
||
export const Scoped: Story = { | ||
render: function ThemeScoped(props) { | ||
return ( | ||
<div> | ||
<ScopedComponent /> | ||
|
||
<ThemeProvider {...props}> | ||
<div style={{border: '1px red dotted', padding: 10, marginBlockStart: 10}}> | ||
<Text>Inside scoped theme provider</Text> | ||
<ScopedComponent /> | ||
</div> | ||
</ThemeProvider> | ||
</div> | ||
); | ||
}, | ||
argTypes: { | ||
scoped: { | ||
table: { | ||
disable: true, | ||
}, | ||
}, | ||
}, | ||
}; |
This file contains 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,4 @@ | ||
export type RealTheme = 'light' | 'light-hc' | 'dark' | 'dark-hc' | string; | ||
export type RealTheme = 'light' | 'light-hc' | 'dark' | 'dark-hc' | (string & {}); | ||
export type ThemeType = 'light' | 'dark'; | ||
export type Theme = 'system' | RealTheme; | ||
export type Direction = 'ltr' | 'rtl'; |
This file contains 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,8 +1,6 @@ | ||
import React from 'react'; | ||
|
||
import {ThemeContext} from './ThemeContext'; | ||
import type {ThemeContextProps} from './ThemeContext'; | ||
import {useThemeContext} from './useThemeContext'; | ||
|
||
export function useDirection(): ThemeContextProps['direction'] { | ||
return React.useContext(ThemeContext).direction; | ||
return useThemeContext().direction; | ||
} |
This file contains 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,8 +1,6 @@ | ||
import React from 'react'; | ||
|
||
import {ThemeContext} from './ThemeContext'; | ||
import type {ThemeContextProps} from './ThemeContext'; | ||
import {useThemeContext} from './useThemeContext'; | ||
|
||
export function useTheme(): ThemeContextProps['theme'] { | ||
return React.useContext(ThemeContext).theme; | ||
return useThemeContext().theme; | ||
} |
This file contains 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,11 @@ | ||
import React from 'react'; | ||
|
||
import {ThemeContext} from './ThemeContext'; | ||
|
||
export function useThemeContext() { | ||
const state = React.useContext(ThemeContext); | ||
if (state === undefined) { | ||
throw new Error('useTheme* hooks must be used within ThemeProvider'); | ||
} | ||
return state; | ||
} |
This file contains 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,7 +1,6 @@ | ||
import React from 'react'; | ||
|
||
import {ThemeContext, ThemeContextProps} from './ThemeContext'; | ||
import type {ThemeContextProps} from './ThemeContext'; | ||
import {useThemeContext} from './useThemeContext'; | ||
|
||
export function useThemeValue(): ThemeContextProps['themeValue'] { | ||
return React.useContext(ThemeContext).themeValue; | ||
return useThemeContext().themeValue; | ||
} |
This file contains 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,26 +1,23 @@ | ||
import React from 'react'; | ||
|
||
import type {Subtract} from 'utility-types'; | ||
|
||
import {getComponentName} from '../utils/getComponentName'; | ||
|
||
import {ThemeContext} from './ThemeContext'; | ||
import type {ThemeContextProps} from './ThemeContext'; | ||
import {useDirection} from './useDirection'; | ||
|
||
export interface WithDirectionProps extends Pick<ThemeContextProps, 'direction'> {} | ||
|
||
export function withDirection<T extends WithDirectionProps>( | ||
WrappedComponent: React.ComponentType<T>, | ||
): React.ComponentType<Subtract<T, WithDirectionProps>> { | ||
): React.ComponentType<Omit<T, keyof WithDirectionProps>> { | ||
const componentName = getComponentName(WrappedComponent); | ||
|
||
return class WithDirectionComponent extends React.Component<Subtract<T, WithDirectionProps>> { | ||
static displayName = `withDirection(${componentName})`; | ||
static contextType = ThemeContext; | ||
context!: React.ContextType<typeof ThemeContext>; | ||
|
||
render() { | ||
return <WrappedComponent {...(this.props as T)} direction={this.context.direction} />; | ||
} | ||
const component = function WithDirectionComponent(props: Omit<T, keyof WithDirectionProps>) { | ||
const direction = useDirection(); | ||
return <WrappedComponent {...(props as T)} direction={direction} />; | ||
}; | ||
|
||
component.displayName = `withDirection(${componentName})`; | ||
|
||
return component; | ||
} |
This file contains 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,26 +1,23 @@ | ||
import React from 'react'; | ||
|
||
import type {Subtract} from 'utility-types'; | ||
|
||
import {getComponentName} from '../utils/getComponentName'; | ||
|
||
import {ThemeContext} from './ThemeContext'; | ||
import type {ThemeContextProps} from './ThemeContext'; | ||
import {useTheme} from './useTheme'; | ||
|
||
export interface WithThemeProps extends Pick<ThemeContextProps, 'theme'> {} | ||
|
||
export function withTheme<T extends WithThemeProps>( | ||
WrappedComponent: React.ComponentType<T>, | ||
): React.ComponentType<Subtract<T, WithThemeProps>> { | ||
): React.ComponentType<Omit<T, keyof WithThemeProps>> { | ||
const componentName = getComponentName(WrappedComponent); | ||
|
||
return class WithThemeComponent extends React.Component<Subtract<T, WithThemeProps>> { | ||
static displayName = `withTheme(${componentName})`; | ||
static contextType = ThemeContext; | ||
context!: React.ContextType<typeof ThemeContext>; | ||
|
||
render() { | ||
return <WrappedComponent {...(this.props as T)} theme={this.context.theme} />; | ||
} | ||
const component = function WithThemeComponent(props: Omit<T, keyof WithThemeProps>) { | ||
const theme = useTheme(); | ||
return <WrappedComponent {...(props as T)} theme={theme} />; | ||
}; | ||
|
||
component.displayName = `withTheme(${componentName})`; | ||
|
||
return component; | ||
} |
This file contains 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,26 +1,23 @@ | ||
import React from 'react'; | ||
|
||
import type {Subtract} from 'utility-types'; | ||
|
||
import {getComponentName} from '../utils/getComponentName'; | ||
|
||
import {ThemeContext} from './ThemeContext'; | ||
import type {ThemeContextProps} from './ThemeContext'; | ||
import {useThemeValue} from './useThemeValue'; | ||
|
||
export interface WithThemeValueProps extends Pick<ThemeContextProps, 'themeValue'> {} | ||
|
||
export function withThemeValue<T extends WithThemeValueProps>( | ||
WrappedComponent: React.ComponentType<T>, | ||
): React.ComponentType<Subtract<T, WithThemeValueProps>> { | ||
): React.ComponentType<Omit<T, keyof WithThemeValueProps>> { | ||
const componentName = getComponentName(WrappedComponent); | ||
|
||
return class WithThemeValueComponent extends React.Component<Subtract<T, WithThemeValueProps>> { | ||
static displayName = `withThemeValue(${componentName})`; | ||
static contextType = ThemeContext; | ||
context!: React.ContextType<typeof ThemeContext>; | ||
|
||
render() { | ||
return <WrappedComponent {...(this.props as T)} themeValue={this.context.themeValue} />; | ||
} | ||
const component = function WithThemeValueComponent(props: Omit<T, keyof WithThemeValueProps>) { | ||
const themeValue = useThemeValue(); | ||
return <WrappedComponent {...(props as T)} themeValue={themeValue} />; | ||
}; | ||
|
||
component.displayName = `withThemeValue(${componentName})`; | ||
|
||
return component; | ||
} |
This file contains 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 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