-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wrap spectrum View, Text and Heading to accept custom colors (#…
…1903) - Exposes our extra semantic colors to these components - Created colorUtils in theme, containing theme variables - Move spectrumUtils to spectrum/utils (was originally going to put it here) - Added eslint config to allow using UNSAFE_styles/UNSAFE_classname Tested with: ```html <Text color="red">css red</Text> <Text color="red-1000">theme red-1000</Text> <Text color="accent">accent</Text> <Text color="positive">positive</Text> <Text color="negative">negative</Text> <Text color="info">info</Text> <Text color="notice">notice</Text> <Text color="bg">bg</Text> <Text color="color-mix(in srgb, red, blue)">purple</Text> ```
- Loading branch information
Showing
26 changed files
with
502 additions
and
25 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
6 changes: 4 additions & 2 deletions
6
packages/code-studio/src/styleguide/RandomAreaPlotAnimation.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
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
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,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 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 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 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 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 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 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
2 changes: 1 addition & 1 deletion
2
...ages/components/src/SpectrumUtils.test.ts → ...ges/components/src/spectrum/utils.test.ts
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
2 changes: 1 addition & 1 deletion
2
packages/components/src/SpectrumUtils.ts → packages/components/src/spectrum/utils.ts
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,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.