-
Notifications
You must be signed in to change notification settings - Fork 33
feat: wrap spectrum View, Text and Heading to accept custom colors #1903
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0ce838a
feat: wrap spectrum View, Text and Heading to accept custom colors
dsmmcken beac2a0
should probably export these... doh
dsmmcken 1ebe5e4
address feedback
dsmmcken 1d89e63
whoops, was just looking
dsmmcken 4fd1571
non-exhaustively remove some of the uses of spectrum outside spectrum…
dsmmcken ad077e0
Merge remote-tracking branch 'origin/main' into dmckenzie_view/text
dsmmcken 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 hidden or 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
6 changes: 4 additions & 2 deletions
6
packages/code-studio/src/styleguide/RandomAreaPlotAnimation.tsx
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,25 @@ | ||
| import React from 'react'; | ||
| import { render } from '@testing-library/react'; | ||
| import { Heading } from './Heading'; | ||
|
|
||
| describe('Heading', () => { | ||
| it('mounts and unmounts', () => { | ||
| render(<Heading>{null}</Heading>); | ||
| }); | ||
|
|
||
| it('renders without color', () => { | ||
| const { getByTestId } = render(<Heading data-testid="Heading" />); | ||
| const HeadingElement = getByTestId('Heading'); | ||
| expect(HeadingElement).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('renders with color', () => { | ||
| const color = 'red'; | ||
| const { getByTestId } = render( | ||
| <Heading data-testid="Heading" color={color} /> | ||
| ); | ||
| const HeadingElement = getByTestId('Heading'); | ||
| expect(HeadingElement).toBeInTheDocument(); | ||
| expect(HeadingElement).toHaveStyle(`color: ${color}`); | ||
| }); | ||
| }); |
This file contains hidden or 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,36 @@ | ||
| /* eslint-disable react/jsx-props-no-spreading */ | ||
| import { useMemo } from 'react'; | ||
| import { | ||
| Heading as SpectrumHeading, | ||
| type HeadingProps as SpectrumHeadingProps, | ||
| } from '@adobe/react-spectrum'; | ||
| import { type ColorValue, colorValueStyle } from '../theme/colorUtils'; | ||
|
|
||
| export type HeadingProps = SpectrumHeadingProps & { | ||
| color?: ColorValue; | ||
| }; | ||
|
|
||
| /** | ||
| * A Heading component that re-exports the Spectrum Heading component. | ||
| * It overrides ColorValues to accept CSS color strings and custom | ||
| * variable names from our color paletee and semantic colors. | ||
| * | ||
| * @param props The props for the Heading component | ||
| * @returns The Heading component | ||
| * | ||
| */ | ||
|
|
||
| export function Heading(props: HeadingProps): JSX.Element { | ||
| const { color, UNSAFE_style, ...rest } = props; | ||
| const style = useMemo( | ||
| () => ({ | ||
| ...UNSAFE_style, | ||
| color: colorValueStyle(color), | ||
| }), | ||
| [color, UNSAFE_style] | ||
| ); | ||
|
|
||
| return <SpectrumHeading {...rest} UNSAFE_style={style} />; | ||
| } | ||
|
|
||
| export default Heading; |
This file contains hidden or 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,27 @@ | ||
| import React from 'react'; | ||
| import { render } from '@testing-library/react'; | ||
| import { Text } from './Text'; | ||
|
|
||
| describe('Text', () => { | ||
| it('mounts and unmounts', () => { | ||
| render(<Text>test</Text>); | ||
| }); | ||
|
|
||
| it('renders without color', () => { | ||
| const { getByTestId } = render(<Text data-testid="Text">test</Text>); | ||
| const TextElement = getByTestId('Text'); | ||
| expect(TextElement).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('renders with color', () => { | ||
| const color = 'red'; | ||
| const { getByTestId } = render( | ||
| <Text data-testid="Text" color={color}> | ||
| test | ||
| </Text> | ||
| ); | ||
| const TextElement = getByTestId('Text'); | ||
| expect(TextElement).toBeInTheDocument(); | ||
| expect(TextElement).toHaveStyle(`color: ${color}`); | ||
| }); | ||
| }); |
This file contains hidden or 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,36 @@ | ||
| /* eslint-disable react/jsx-props-no-spreading */ | ||
| import { useMemo } from 'react'; | ||
| import { | ||
| Text as SpectrumText, | ||
| type TextProps as SpectrumTextProps, | ||
| } from '@adobe/react-spectrum'; | ||
| import { type ColorValue, colorValueStyle } from '../theme/colorUtils'; | ||
|
|
||
| export type TextProps = SpectrumTextProps & { | ||
| color?: ColorValue; | ||
| }; | ||
|
|
||
| /** | ||
| * A Text component that re-exports the Spectrum Text component. | ||
| * It overrides ColorValues to accept CSS color strings and custom | ||
| * variable names from our color paletee and semantic colors. | ||
| * | ||
| * @param props The props for the Text component | ||
| * @returns The Text component | ||
| * | ||
| */ | ||
|
|
||
| export function Text(props: TextProps): JSX.Element { | ||
| const { color, UNSAFE_style, ...rest } = props; | ||
| const style = useMemo( | ||
| () => ({ | ||
| ...UNSAFE_style, | ||
| color: colorValueStyle(color), | ||
| }), | ||
| [color, UNSAFE_style] | ||
| ); | ||
|
|
||
| return <SpectrumText {...rest} UNSAFE_style={style} />; | ||
| } | ||
|
|
||
| export default Text; |
This file contains hidden or 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,25 @@ | ||
| import React from 'react'; | ||
| import { render } from '@testing-library/react'; | ||
| import { View } from './View'; | ||
|
|
||
| describe('View', () => { | ||
| it('mounts and unmounts', () => { | ||
| render(<View>{null}</View>); | ||
| }); | ||
|
|
||
| it('renders without backgroundColor', () => { | ||
| const { getByTestId } = render(<View data-testid="view" />); | ||
| const viewElement = getByTestId('view'); | ||
| expect(viewElement).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('renders with backgroundColor', () => { | ||
| const backgroundColor = 'red'; | ||
| const { getByTestId } = render( | ||
| <View data-testid="view" backgroundColor={backgroundColor} /> | ||
| ); | ||
| const viewElement = getByTestId('view'); | ||
| expect(viewElement).toBeInTheDocument(); | ||
| expect(viewElement).toHaveStyle(`background-color: ${backgroundColor}`); | ||
| }); | ||
| }); |
This file contains hidden or 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,36 @@ | ||
| /* eslint-disable react/jsx-props-no-spreading */ | ||
| import { useMemo } from 'react'; | ||
| import { | ||
| View as SpectrumView, | ||
| type ViewProps as SpectrumViewProps, | ||
| } from '@adobe/react-spectrum'; | ||
| import { type ColorValue, colorValueStyle } from '../theme/colorUtils'; | ||
|
|
||
| export type ViewProps = Omit<SpectrumViewProps<6>, 'backgroundColor'> & { | ||
| backgroundColor?: ColorValue; | ||
| }; | ||
|
|
||
| /** | ||
| * A View component that re-exports the Spectrum View component. | ||
| * However, it overrides ColorValues to accept CSS color strings and | ||
| * our custom variable names from our color paletee and semantic colors. | ||
| * | ||
| * @param props The props for the View component | ||
| * @returns The View component | ||
| * | ||
| */ | ||
|
|
||
| export function View(props: ViewProps): JSX.Element { | ||
| const { backgroundColor, UNSAFE_style, ...rest } = props; | ||
| const style = useMemo( | ||
| () => ({ | ||
| ...UNSAFE_style, | ||
| backgroundColor: colorValueStyle(backgroundColor), | ||
| }), | ||
| [backgroundColor, UNSAFE_style] | ||
| ); | ||
|
|
||
| return <SpectrumView {...rest} UNSAFE_style={style} />; | ||
| } | ||
|
|
||
| export default View; |
File renamed without changes.
This file contains hidden or 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 hidden or 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 hidden or 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
2 changes: 1 addition & 1 deletion
2
...ages/components/src/SpectrumUtils.test.ts → ...ges/components/src/spectrum/utils.test.ts
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
packages/components/src/SpectrumUtils.ts → packages/components/src/spectrum/utils.ts
This file contains hidden or 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 hidden or 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 hidden or 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,15 @@ | ||
| import { colorValueStyle } from './colorUtils'; | ||
|
|
||
| describe('ColorValues', () => { | ||
| test('should return the correct color style', () => { | ||
| // dh-color variables | ||
| expect(colorValueStyle('blue-1000')).toBe('var(--dh-color-blue-1000)'); | ||
| expect(colorValueStyle('accent-1000')).toBe('var(--dh-color-accent-1000)'); | ||
| expect(colorValueStyle('bg')).toBe('var(--dh-color-bg)'); | ||
| // pass-through variables | ||
| expect(colorValueStyle('red')).toBe('red'); | ||
| expect(colorValueStyle('rgb(255, 0, 0)')).toBe('rgb(255, 0, 0)'); | ||
| expect(colorValueStyle('#ff0000')).toBe('#ff0000'); | ||
| expect(colorValueStyle(undefined)).toBe(undefined); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.