-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Theming): add
VisibilityByTheme
as a shared component (#2280)
Co-authored-by: Anders <[email protected]>
- Loading branch information
1 parent
02656a7
commit 2592658
Showing
10 changed files
with
277 additions
and
22 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
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
11 changes: 0 additions & 11 deletions
11
packages/dnb-design-system-portal/src/shared/tags/FilterByTheme.tsx
This file was deleted.
Oops, something went wrong.
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,63 @@ | ||
import React from 'react' | ||
import useTheme from './useTheme' | ||
import type { ThemeNames, ThemeProps } from './Theme' | ||
|
||
type VisibilityByThemeProps = { | ||
/** | ||
* A valid theme name or object. | ||
* Will pass children on a match. | ||
*/ | ||
visible?: ThemeParams | ||
|
||
/** | ||
* A valid theme name or object. | ||
* Will omit passing children on a match. | ||
* NB: "visible" takes presense over "hidden" | ||
*/ | ||
hidden?: ThemeParams | ||
|
||
/** | ||
* Any kind of a React Node that should render on a match. | ||
*/ | ||
children: React.ReactNode | ||
} | ||
|
||
type ThemeItem = ThemeNames | ThemeProps | ||
type ThemeParams = ThemeItem | Array<ThemeItem> | ||
|
||
export default function VisibilityByTheme({ | ||
children, | ||
visible, | ||
hidden, | ||
}: VisibilityByThemeProps) { | ||
const theme = useTheme() | ||
|
||
const visibleList = Array.isArray(visible) ? visible : [visible] | ||
const hiddenList = Array.isArray(hidden) ? hidden : [hidden] | ||
|
||
if (visible) { | ||
if (!visibleList.some(match(theme))) { | ||
return null | ||
} | ||
} else if (hidden) { | ||
if (hiddenList.some(match(theme))) { | ||
return null | ||
} | ||
} | ||
|
||
return children as JSX.Element | ||
|
||
function match(theme: ThemeProps) { | ||
return (themeItem: ThemeItem) => { | ||
return typeof themeItem === 'string' | ||
? theme.name === themeItem | ||
: matchObject(theme, themeItem) | ||
} | ||
} | ||
|
||
function matchObject(theme: ThemeProps, themeItem: ThemeItem) { | ||
return Object.keys(themeItem).every((key) => { | ||
return theme[key] === themeItem[key] | ||
}) | ||
} | ||
} |
159 changes: 159 additions & 0 deletions
159
packages/dnb-eufemia/src/shared/__tests__/VisibilityByTheme.test.tsx
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,159 @@ | ||
import React from 'react' | ||
import { render } from '@testing-library/react' | ||
import Theme from '../Theme' | ||
import VisibilityByTheme from '../VisibilityByTheme' | ||
|
||
describe('VisibilityByTheme', () => { | ||
it('renders content if not visible or hidden was given', () => { | ||
const Component = (props) => ( | ||
<Theme name="eiendom" {...props}> | ||
<VisibilityByTheme> | ||
<p>I'm visible</p> | ||
</VisibilityByTheme> | ||
</Theme> | ||
) | ||
|
||
render(<Component />) | ||
|
||
expect(document.body.textContent).toBe("I'm visible") | ||
}) | ||
|
||
it('renders content on name match', () => { | ||
const Component = (props) => ( | ||
<Theme name="eiendom" {...props}> | ||
<VisibilityByTheme visible="eiendom"> | ||
<p>I'm visible</p> | ||
</VisibilityByTheme> | ||
</Theme> | ||
) | ||
|
||
const { rerender } = render(<Component />) | ||
|
||
expect(document.body.textContent).toBe("I'm visible") | ||
|
||
rerender(<Component name="sbanken" />) | ||
|
||
expect(document.body.textContent).toBe('') | ||
}) | ||
|
||
it('skips render when hidden matches', () => { | ||
const Component = (props) => ( | ||
<Theme name="eiendom" {...props}> | ||
<VisibilityByTheme hidden="sbanken"> | ||
<p>I'm visible</p> | ||
</VisibilityByTheme> | ||
</Theme> | ||
) | ||
|
||
const { rerender } = render(<Component />) | ||
|
||
expect(document.body.textContent).toBe("I'm visible") | ||
|
||
rerender(<Component name="sbanken" />) | ||
|
||
expect(document.body.textContent).toBe('') | ||
|
||
rerender(<Component name="ui" />) | ||
|
||
expect(document.body.textContent).toBe("I'm visible") | ||
}) | ||
|
||
it('prefers visible over hidden', () => { | ||
const Component = (props) => ( | ||
<Theme name="eiendom" {...props}> | ||
<VisibilityByTheme visible="eiendom" hidden="sbanken"> | ||
<p>I'm visible</p> | ||
</VisibilityByTheme> | ||
</Theme> | ||
) | ||
|
||
const { rerender } = render(<Component />) | ||
|
||
expect(document.body.textContent).toBe("I'm visible") | ||
|
||
rerender(<Component name="sbanken" />) | ||
|
||
expect(document.body.textContent).toBe('') | ||
|
||
rerender(<Component name="eiendom" />) | ||
|
||
expect(document.body.textContent).toBe("I'm visible") | ||
}) | ||
|
||
it('renders content on match from names in an array', () => { | ||
const Component = (props) => ( | ||
<Theme name="eiendom" {...props}> | ||
<VisibilityByTheme visible={['eiendom', 'sbanken']}> | ||
<p>I'm visible</p> | ||
</VisibilityByTheme> | ||
</Theme> | ||
) | ||
|
||
const { rerender } = render(<Component />) | ||
|
||
expect(document.body.textContent).toBe("I'm visible") | ||
|
||
rerender(<Component name="sbanken" />) | ||
|
||
expect(document.body.textContent).toBe("I'm visible") | ||
|
||
rerender(<Component name="ui" />) | ||
|
||
expect(document.body.textContent).toBe('') | ||
}) | ||
|
||
it('renders content on match from names in an object inside an array', () => { | ||
const Component = (props) => ( | ||
<Theme name="eiendom" {...props}> | ||
<VisibilityByTheme | ||
visible={[{ name: 'eiendom' }, { name: 'sbanken' }]} | ||
> | ||
<p>I'm visible</p> | ||
</VisibilityByTheme> | ||
</Theme> | ||
) | ||
|
||
const { rerender } = render(<Component />) | ||
|
||
expect(document.body.textContent).toBe("I'm visible") | ||
|
||
rerender(<Component name="sbanken" />) | ||
|
||
expect(document.body.textContent).toBe("I'm visible") | ||
|
||
rerender(<Component name="ui" />) | ||
|
||
expect(document.body.textContent).toBe('') | ||
}) | ||
|
||
it('renders content on match by several theme criterias', () => { | ||
const Component = (props) => ( | ||
<Theme name="eiendom" variant="red" {...props}> | ||
<VisibilityByTheme | ||
visible={[ | ||
{ name: 'eiendom', variant: 'red' }, | ||
{ name: 'sbanken', variant: 'blue' }, | ||
]} | ||
> | ||
<p>I'm visible</p> | ||
</VisibilityByTheme> | ||
</Theme> | ||
) | ||
|
||
const { rerender } = render(<Component />) | ||
|
||
expect(document.body.textContent).toBe("I'm visible") | ||
|
||
rerender(<Component name="sbanken" />) | ||
|
||
expect(document.body.textContent).toBe('') | ||
|
||
rerender(<Component name="sbanken" variant="blue" />) | ||
|
||
expect(document.body.textContent).toBe("I'm visible") | ||
|
||
rerender(<Component name="sbanken" variant="red" />) | ||
|
||
expect(document.body.textContent).toBe('') | ||
}) | ||
}) |
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