-
Notifications
You must be signed in to change notification settings - Fork 90
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
feat(theming): add componentStyles utility #1986
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b579307
deps: upgrade to styled-component v6
ze-flo 60b14bf
test: fix specs
ze-flo 7d57f43
chore: fix type errors
ze-flo 6806ba3
Merge branch 'main' of github.com:zendeskgarden/react-components into…
ze-flo c3eb8e3
test: fix specs
ze-flo eeee419
refactor: PR feedback
ze-flo 6b59c31
Merge branch 'main' of github.com:zendeskgarden/react-components into…
ze-flo 10dc96a
chore: fix type error
ze-flo d49e6f3
feat(theming): add `componentStyles` utility
jzempel 6021148
Add unit testing
jzempel b2bfb41
Merge branch 'main' into jzempel/component-styles
jzempel d264041
Revert API doc formatting
jzempel e06ab51
Update JS doc
jzempel 2ee9a0a
Address TS feedback from @ze-flo
jzempel 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 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
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.
Is
/** @component */
needed for the documentation?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.
nah, just leftover from ancient Styleguidist notation