-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(swatch-picker): Added ColorSwatch sub component and selection lo…
…gic (#30544) Co-authored-by: Oleksandr Fediashov <alexander.mcgarret@gmail.com>
- Loading branch information
1 parent
be0c9ec
commit ae14927
Showing
25 changed files
with
685 additions
and
53 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-shared-contexts-363c88cd-d979-4cea-a182-2792a0a16c31.json
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,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "feat(swatch-picker): Added plain ColorSwatch sub component.", | ||
"packageName": "@fluentui/react-shared-contexts", | ||
"email": "vkozlova@microsoft.com", | ||
"dependentChangeType": "patch" | ||
} |
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
1 change: 1 addition & 0 deletions
1
packages/react-components/react-swatch-picker-preview/src/ColorSwatch.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './components/ColorSwatch/index'; |
42 changes: 42 additions & 0 deletions
42
...ct-components/react-swatch-picker-preview/src/components/ColorSwatch/ColorSwatch.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,42 @@ | ||
import * as React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import { isConformant } from '../../testing/isConformant'; | ||
import { ColorSwatch } from './ColorSwatch'; | ||
import { colorSwatchClassNames } from './useColorSwatchStyles.styles'; | ||
|
||
describe('ColorSwatch', () => { | ||
isConformant({ | ||
Component: ColorSwatch, | ||
displayName: 'ColorSwatch', | ||
primarySlot: 'button', | ||
testOptions: { | ||
'has-static-classnames': [ | ||
{ | ||
props: {}, | ||
expectedClassNames: { | ||
button: colorSwatchClassNames.button, | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
it('renders a default state', () => { | ||
const result = render(<ColorSwatch color="#f09" value="#f09" />); | ||
expect(result.container).toMatchInlineSnapshot(` | ||
<div> | ||
<div | ||
aria-selected="false" | ||
class="fui-ColorSwatch" | ||
role="radio" | ||
style="--fui-SwatchPicker--color: #f09;" | ||
> | ||
<button | ||
class="fui-ColorSwatch__button" | ||
type="button" | ||
/> | ||
</div> | ||
</div> | ||
`); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
...s/react-components/react-swatch-picker-preview/src/components/ColorSwatch/ColorSwatch.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,22 @@ | ||
import * as React from 'react'; | ||
import type { ForwardRefComponent } from '@fluentui/react-utilities'; | ||
// import { useCustomStyleHook_unstable } from '@fluentui/react-shared-contexts'; | ||
import { useColorSwatch_unstable } from './useColorSwatch'; | ||
import { renderColorSwatch_unstable } from './renderColorSwatch'; | ||
import { useColorSwatchStyles_unstable } from './useColorSwatchStyles.styles'; | ||
import type { ColorSwatchProps } from './ColorSwatch.types'; | ||
|
||
/** | ||
* ColorSwatch component - TODO: add more docs | ||
*/ | ||
export const ColorSwatch: ForwardRefComponent<ColorSwatchProps> = React.forwardRef((props, ref) => { | ||
const state = useColorSwatch_unstable(props, ref); | ||
|
||
useColorSwatchStyles_unstable(state); | ||
// TODO uncomment when SwatchPicker is stable | ||
// useCustomStyleHook_unstable('useColorSwatchStyles_unstable')(state); | ||
|
||
return renderColorSwatch_unstable(state); | ||
}); | ||
|
||
ColorSwatch.displayName = 'ColorSwatch'; |
31 changes: 31 additions & 0 deletions
31
...ct-components/react-swatch-picker-preview/src/components/ColorSwatch/ColorSwatch.types.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { ComponentProps, ComponentState, Slot } from '@fluentui/react-utilities'; | ||
import { SwatchPickerProps } from '../SwatchPicker/SwatchPicker.types'; | ||
|
||
export type ColorSwatchSlots = { | ||
root: NonNullable<Slot<'div'>>; | ||
button: NonNullable<Slot<'button'>>; | ||
}; | ||
|
||
/** | ||
* ColorSwatch Props | ||
*/ | ||
export type ColorSwatchProps = Omit<ComponentProps<Partial<ColorSwatchSlots>, 'button'>, 'children'> & | ||
Pick<SwatchPickerProps, 'size' | 'shape'> & { | ||
/** | ||
* Swatch color | ||
*/ | ||
color: string; | ||
|
||
/** | ||
* Swatch value | ||
*/ | ||
value: string; | ||
}; | ||
|
||
/** | ||
* State used in rendering ColorSwatch | ||
*/ | ||
export type ColorSwatchState = ComponentState<ColorSwatchSlots> & | ||
Pick<ColorSwatchProps, 'color' | 'size' | 'shape' | 'value'> & { | ||
selected: boolean; | ||
}; |
5 changes: 5 additions & 0 deletions
5
packages/react-components/react-swatch-picker-preview/src/components/ColorSwatch/index.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from './ColorSwatch'; | ||
export * from './ColorSwatch.types'; | ||
export * from './renderColorSwatch'; | ||
export * from './useColorSwatch'; | ||
export * from './useColorSwatchStyles.styles'; |
18 changes: 18 additions & 0 deletions
18
...t-components/react-swatch-picker-preview/src/components/ColorSwatch/renderColorSwatch.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,18 @@ | ||
/** @jsxRuntime automatic */ | ||
/** @jsxImportSource @fluentui/react-jsx-runtime */ | ||
|
||
import { assertSlots } from '@fluentui/react-utilities'; | ||
import type { ColorSwatchState, ColorSwatchSlots } from './ColorSwatch.types'; | ||
|
||
/** | ||
* Render the final JSX of ColorSwatch | ||
*/ | ||
export const renderColorSwatch_unstable = (state: ColorSwatchState) => { | ||
assertSlots<ColorSwatchSlots>(state); | ||
|
||
return ( | ||
<state.root> | ||
<state.button /> | ||
</state.root> | ||
); | ||
}; |
86 changes: 86 additions & 0 deletions
86
...react-components/react-swatch-picker-preview/src/components/ColorSwatch/useColorSwatch.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import * as React from 'react'; | ||
import { slot, useEventCallback, getPartitionedNativeProps } from '@fluentui/react-utilities'; | ||
import type { ColorSwatchProps, ColorSwatchState } from './ColorSwatch.types'; | ||
import { useFocusWithin } from '@fluentui/react-tabster'; | ||
import { useSwatchPickerContextValue_unstable } from '../../contexts/swatchPicker'; | ||
import { swatchCSSVars } from './useColorSwatchStyles.styles'; | ||
|
||
/** | ||
* Create the state required to render ColorSwatch. | ||
* | ||
* The returned state can be modified with hooks such as useColorSwatchStyles_unstable, | ||
* before being passed to renderColorSwatch_unstable. | ||
* | ||
* @param props - props from this instance of ColorSwatch | ||
* @param ref - reference to root HTMLButtonElement of ColorSwatch | ||
*/ | ||
export const useColorSwatch_unstable = ( | ||
props: ColorSwatchProps, | ||
ref: React.Ref<HTMLButtonElement>, | ||
): ColorSwatchState => { | ||
const { color, value } = props; | ||
const size = useSwatchPickerContextValue_unstable(ctx => ctx.size); | ||
const shape = useSwatchPickerContextValue_unstable(ctx => ctx.shape); | ||
|
||
const requestSelectionChange = useSwatchPickerContextValue_unstable(ctx => ctx.requestSelectionChange); | ||
const selected = useSwatchPickerContextValue_unstable(ctx => ctx.selectedValue === value); | ||
|
||
const onClick = useEventCallback((event: React.MouseEvent<HTMLButtonElement>) => | ||
requestSelectionChange(event, { | ||
selectedValue: value, | ||
selectedColor: color, | ||
}), | ||
); | ||
|
||
const nativeProps = getPartitionedNativeProps({ | ||
props, | ||
primarySlotTagName: 'button', | ||
excludedPropNames: ['value', 'color'], | ||
}); | ||
|
||
const rootVariables = { | ||
[swatchCSSVars.color]: color, | ||
}; | ||
|
||
const root = slot.always(props.root, { | ||
defaultProps: { | ||
ref: useFocusWithin<HTMLDivElement>(), | ||
role: props.role ?? 'radio', | ||
'aria-selected': selected, | ||
...nativeProps.root, | ||
}, | ||
elementType: 'div', | ||
}); | ||
|
||
const button = slot.always(props.button, { | ||
defaultProps: { | ||
ref, | ||
type: 'button', | ||
onClick, | ||
...nativeProps.primary, | ||
}, | ||
elementType: 'button', | ||
}); | ||
|
||
const state: ColorSwatchState = { | ||
components: { | ||
root: 'div', | ||
button: 'button', | ||
}, | ||
root, | ||
button, | ||
size, | ||
shape, | ||
selected, | ||
color, | ||
value, | ||
}; | ||
|
||
// Root props | ||
state.root.style = { | ||
...rootVariables, | ||
...state.root.style, | ||
}; | ||
|
||
return state; | ||
}; |
Oops, something went wrong.