-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from ericrav/theme-override
ThemeOverride
- Loading branch information
Showing
24 changed files
with
305 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Binary file added
BIN
+67.4 KB
.yarn/cache/@testing-library-jest-dom-npm-6.5.0-fa5d3458cd-c2d14103eb.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+96.4 KB
.yarn/cache/dom-accessibility-api-npm-0.6.3-0345e4dede-c325b5144b.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,2 @@ | ||
releases: | ||
theme-party: minor |
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 @@ | ||
import '@testing-library/jest-dom' |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export { ThemeParty } from "./ThemeParty"; | ||
export { costumed } from "./react/costumed"; | ||
export { ThemeProvider } from "./react/ThemeProvider"; | ||
export { ThemeOverride } from "./react/ThemeOverride"; | ||
export { useTheme } from "./react/useTheme"; | ||
export { UserTheme } from "./types"; | ||
export { ThemeExtract } from './ThemeParty.types'; | ||
export { UserTheme, ThemeOfParty, DefaultTheme, DefaultThemeParty, ThemePartyConfig } from "./types"; | ||
export { ThemeExtract, ThemeConfig } from './ThemeParty.types'; |
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,26 @@ | ||
import { useContext, useMemo } from 'react'; | ||
import { DefaultTheme, ThemePartyConfig } from '../types'; | ||
import { themePartyContext } from './context'; | ||
|
||
export interface ThemeOverrideProps<T extends {}> { | ||
/** Override values of current theme. Object should be stable across renders */ | ||
value: ThemePartyConfig<T>; | ||
children: React.ReactNode; | ||
} | ||
|
||
export function ThemeOverride<T extends {} = DefaultTheme>({ | ||
value, | ||
children, | ||
}: ThemeOverrideProps<T>) { | ||
const themeParty = useContext(themePartyContext); | ||
if (!themeParty) | ||
throw new Error('ThemeOverride must be used within a ThemeProvider'); | ||
|
||
const override = useMemo(() => themeParty.createTheme(value), [themeParty, value]); | ||
|
||
return ( | ||
<themePartyContext.Provider value={override}> | ||
{children} | ||
</themePartyContext.Provider> | ||
); | ||
} |
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,47 @@ | ||
import { render } from '@testing-library/react'; | ||
import { ThemeParty } from '../../ThemeParty'; | ||
import { useTheme } from '../useTheme'; | ||
import { ThemeProvider } from '../ThemeProvider'; | ||
import { ThemeOverride } from '../ThemeOverride'; | ||
import { ThemeOfParty } from '../../types'; | ||
|
||
const themeParty = new ThemeParty({ | ||
color: 'red', | ||
fontWeight: 700, | ||
}); | ||
|
||
type Theme = ThemeOfParty<typeof themeParty>; | ||
|
||
const theme2 = themeParty.createTheme({ | ||
color: 'blue', | ||
}); | ||
|
||
const Text = ({ children }: { children: React.ReactNode }) => { | ||
const { color, fontWeight } = useTheme<typeof themeParty>(); | ||
return <p style={{ color, fontWeight }}>{children}</p>; | ||
}; | ||
|
||
test('ThemeOverride', () => { | ||
const { getByText } = render( | ||
<ThemeProvider theme={theme2}> | ||
<Text>Text 1</Text> | ||
|
||
<ThemeOverride<Theme> value={{ color: 'green' }}> | ||
<Text>Text 2</Text> | ||
</ThemeOverride> | ||
|
||
<ThemeOverride<Theme> value={{ fontWeight: () => 400 }}> | ||
<Text>Text 3</Text> | ||
</ThemeOverride> | ||
</ThemeProvider> | ||
); | ||
|
||
expect(getByText('Text 1')).toHaveStyle('color: blue'); | ||
expect(getByText('Text 1')).toHaveStyle('font-weight: 700'); | ||
|
||
expect(getByText('Text 2')).toHaveStyle('color: green'); | ||
expect(getByText('Text 2')).toHaveStyle('font-weight: 700'); | ||
|
||
expect(getByText('Text 3')).toHaveStyle('color: blue'); | ||
expect(getByText('Text 3')).toHaveStyle('font-weight: 400'); | ||
}); |
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,5 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"include": ["src/**/*"], | ||
"exclude": ["**/__tests__/**"], | ||
} |
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 |
---|---|---|
|
@@ -15,5 +15,5 @@ | |
"strict": true, | ||
"skipLibCheck": true | ||
}, | ||
"include": ["src/**/*"] | ||
"include": ["src/**/*", "./jest.setup.ts"] | ||
} |
Oops, something went wrong.