-
Notifications
You must be signed in to change notification settings - Fork 91
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
componentStyles
utility (#1986)
Co-authored-by: Florent Mathieu <[email protected]>
- Loading branch information
Showing
5 changed files
with
85 additions
and
8 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,42 @@ | ||
/** | ||
* Copyright Zendesk, Inc. | ||
* | ||
* Use of this source code is governed under the Apache License, Version 2.0 | ||
* found at http://www.apache.org/licenses/LICENSE-2.0. | ||
*/ | ||
|
||
import { componentStyles } from './componentStyles'; | ||
|
||
describe('componentStyles', () => { | ||
const VALUE = 'content: "test";'; | ||
|
||
it('returns component styles from the theme as expected', () => { | ||
const props = { 'data-garden-id': 'test', theme: { components: { test: VALUE } } } as any; | ||
const result = componentStyles(props); | ||
|
||
expect(result).toBe(VALUE); | ||
}); | ||
|
||
it('returns undefined if no component styles are found', () => { | ||
const props = { 'data-garden-id': 'test', theme: {} } as any; | ||
const result = componentStyles(props); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it('handles component styles provided as a function', () => { | ||
const fn = jest.fn().mockReturnValue(VALUE); | ||
const props = { 'data-garden-id': 'test', theme: { components: { test: fn } } } as any; | ||
const result = componentStyles(props); | ||
|
||
expect(result).toBe(VALUE); | ||
}); | ||
|
||
it('accepts a custom component ID', () => { | ||
const componentId = 'custom'; | ||
const props = { theme: { components: { [componentId]: VALUE } } } as any; | ||
const result = componentStyles({ ...props, componentId }); | ||
|
||
expect(result).toBe(VALUE); | ||
}); | ||
}); |
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,35 @@ | ||
/** | ||
* Copyright Zendesk, Inc. | ||
* | ||
* Use of this source code is governed under the Apache License, Version 2.0 | ||
* found at http://www.apache.org/licenses/LICENSE-2.0. | ||
*/ | ||
|
||
import { DataAttributes, DefaultTheme } from 'styled-components'; | ||
|
||
/** | ||
* CSS for component customizations based on `theme.components[componentId]`. | ||
* | ||
* @param {Object} props.theme Provides `components` object use to resolve the given component ID | ||
* @param {String} [props.componentId] Specifies the lookup id for * `theme.components` styles. | ||
* The ID will be inferred from the `'data-garden-id'` attribute if not provided. | ||
* | ||
* @returns component CSS styles | ||
*/ | ||
export const componentStyles = (props: { theme: DefaultTheme; componentId?: string }) => { | ||
let retVal: string | undefined; | ||
const components = props.theme.components; | ||
const componentId = props.componentId || (props as unknown as DataAttributes)['data-garden-id']; | ||
|
||
if (components && componentId) { | ||
retVal = components[componentId]; | ||
|
||
if (typeof retVal === 'function') { | ||
const fn = retVal as (p: { theme: DefaultTheme } & unknown) => string; | ||
|
||
retVal = fn(props); | ||
} | ||
} | ||
|
||
return retVal; | ||
}; |
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