Skip to content
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(swatch-picker): Added ColorSwatch sub component and selection logic #30544

Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
6469d9f
feat(swatch-picker): Added plain ColorSwatch sub component.
ValentinaKozlova Feb 13, 2024
39fbd07
Added Default stories for the SwatchPicker
ValentinaKozlova Feb 13, 2024
ca502c6
Added context and selection logic
ValentinaKozlova Feb 13, 2024
2683cbd
fixed tests
ValentinaKozlova Feb 13, 2024
9ffac87
change files
ValentinaKozlova Feb 13, 2024
4688f05
Renamed methods
ValentinaKozlova Feb 14, 2024
99dd96e
Update packages/react-components/react-swatch-picker-preview/src/comp…
ValentinaKozlova Feb 14, 2024
43daf64
Update packages/react-components/react-swatch-picker-preview/stories/…
ValentinaKozlova Feb 15, 2024
3af4bc6
PR fix
ValentinaKozlova Feb 14, 2024
7c57c84
Removed unneeded context.
ValentinaKozlova Feb 15, 2024
cc14224
Added `div` wrapper for the ColorSwatch and useColorSwatchState.tsx
ValentinaKozlova Feb 15, 2024
d94860e
fixed context
ValentinaKozlova Feb 15, 2024
56ea3ce
fixes tests
ValentinaKozlova Feb 15, 2024
912cd77
remoed todo from the tests
ValentinaKozlova Feb 16, 2024
723d5d4
added selectedCOlor to the onSelectionChange method
ValentinaKozlova Feb 16, 2024
32bc856
Updated tests and styles
ValentinaKozlova Feb 16, 2024
fa2a968
fixed type-check
ValentinaKozlova Feb 16, 2024
9ef346a
Merge branch 'master' into feat/swatch-picker-color-swatch
ValentinaKozlova Feb 16, 2024
f3b9564
Update packages/react-components/react-swatch-picker-preview/src/comp…
ValentinaKozlova Feb 19, 2024
7d4527a
Update packages/react-components/react-swatch-picker-preview/src/comp…
ValentinaKozlova Feb 19, 2024
1277c3e
Update packages/react-components/react-swatch-picker-preview/src/comp…
ValentinaKozlova Feb 19, 2024
989cf62
Update packages/react-components/react-swatch-picker-preview/src/comp…
ValentinaKozlova Feb 19, 2024
874a0e5
Update packages/react-components/react-swatch-picker-preview/src/comp…
ValentinaKozlova Feb 19, 2024
4b2d5fd
Update packages/react-components/react-swatch-picker-preview/src/comp…
ValentinaKozlova Feb 19, 2024
5842446
PR fix #2
ValentinaKozlova Feb 19, 2024
6ae6bfb
added new event handlers
ValentinaKozlova Feb 19, 2024
3ed377b
Fixed methods
ValentinaKozlova Feb 20, 2024
4aa214f
fixed type errors
ValentinaKozlova Feb 20, 2024
a122f96
updated api
ValentinaKozlova Feb 20, 2024
31e631c
Merge branch 'master' into feat/swatch-picker-color-swatch
ValentinaKozlova Feb 21, 2024
4aaa27c
updated deps
ValentinaKozlova Feb 21, 2024
9134b58
fix of props order
ValentinaKozlova Feb 21, 2024
d3d2d6a
Merge branch 'master' into feat/swatch-picker-color-swatch
ValentinaKozlova Feb 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,89 @@ import type { Slot } from '@fluentui/react-utilities';
import type { SlotClassNames } from '@fluentui/react-utilities';

// @public
export const renderSwatchPicker_unstable: (state: SwatchPickerState) => JSX.Element;
export const ColorSwatch: ForwardRefComponent<ColorSwatchProps>;

// @public (undocumented)
export const colorSwatchClassNames: SlotClassNames<ColorSwatchSlots>;

// @public
export type ColorSwatchProps = Omit<ComponentProps<Partial<ColorSwatchSlots>, 'button'>, 'children'> & Pick<SwatchPickerProps, 'size' | 'shape'> & {
color: string;
value: string;
};

// @public (undocumented)
export type ColorSwatchSlots = {
root: NonNullable<Slot<'div'>>;
button: NonNullable<Slot<'button'>>;
};

// @public
export type ColorSwatchState = ComponentState<ColorSwatchSlots> & Pick<ColorSwatchProps, 'color' | 'size' | 'shape' | 'value'> & {
selected: boolean;
};

// @public
export const renderColorSwatch_unstable: (state: ColorSwatchState) => JSX.Element;

// @public
export const renderSwatchPicker_unstable: (state: SwatchPickerState, contextValues: SwatchPickerContextValue) => JSX.Element;

// @public (undocumented)
export const swatchCSSVars: {
swatchColor: string;
};

// @public
export const SwatchPicker: ForwardRefComponent<SwatchPickerProps>;

// @public (undocumented)
export const swatchPickerClassNames: SlotClassNames<SwatchPickerSlots>;

// @public (undocumented)
export const swatchPickerCSSVars: {
columnCountGrid: string;
cellSize: string;
gridGap: string;
};

// @public
export type SwatchPickerProps = ComponentProps<SwatchPickerSlots> & {};
export type SwatchPickerProps = ComponentProps<SwatchPickerSlots> & {
defaultSelectedValue?: string;
onSelectionChange?: SwatchPickerSelectEventHandler;
selectedValue?: string;
size?: 'extraSmall' | 'small' | 'medium' | 'large';
shape?: 'rounded' | 'square' | 'circular';
};

// @public (undocumented)
export type SwatchPickerSelectData = {
selectedValue: string;
selectedColor: string;
};

// @public (undocumented)
export type SwatchPickerSelectEvent = React_2.MouseEvent | React_2.KeyboardEvent | React_2.ChangeEvent;

// @public (undocumented)
export type SwatchPickerSelectEventHandler = (event: SwatchPickerSelectEvent, data: SwatchPickerSelectData) => void;

// @public (undocumented)
export type SwatchPickerSlots = {
root: Slot<'div'>;
};

// @public
export type SwatchPickerState = ComponentState<SwatchPickerSlots>;
export type SwatchPickerState = ComponentState<SwatchPickerSlots> & SwatchPickerContextValue & Pick<SwatchPickerProps, 'size' | 'shape'>;

// @public
export const useColorSwatch_unstable: (props: ColorSwatchProps, ref: React_2.Ref<HTMLButtonElement>) => ColorSwatchState;

// @public
export const useColorSwatchStyles_unstable: (state: ColorSwatchState) => ColorSwatchState;

// @public
export const useSwatchPicker_unstable: (props: SwatchPickerProps, ref: React_2.Ref<HTMLDivElement>) => SwatchPickerState;
export const useSwatchPicker_unstable: <T>(props: SwatchPickerProps, ref: React_2.Ref<HTMLDivElement>) => SwatchPickerState;

// @public
export const useSwatchPickerStyles_unstable: (state: SwatchPickerState) => SwatchPickerState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
"@fluentui/scripts-tasks": "*"
},
"dependencies": {
"@fluentui/react-context-selector": "^9.1.51",
"@fluentui/react-jsx-runtime": "^9.0.29",
"@fluentui/react-shared-contexts": "^9.14.0",
"@fluentui/react-tabster": "^9.19.0",
"@fluentui/react-theme": "^9.1.16",
"@fluentui/react-utilities": "^9.18.0",
"@griffel/react": "^1.5.14",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './components/ColorSwatch/index';
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>
`);
});
});
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';
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;
};
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';
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>
);
};
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 { SwatchPickerOnSelectionChangeEvent } from '../SwatchPicker/SwatchPicker.types';
import { useFocusWithin } from '@fluentui/react-tabster';
import { useSwatchPickerContextValue_unstable } from '../../contexts/swatchPicker';
import { swatchCSSVars } from './useColorSwatchStyles.styles';

const { swatchColor } = swatchCSSVars;

/**
* 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: SwatchPickerOnSelectionChangeEvent) =>
requestSelectionChange({ event, selectedValue: value, selectedColor: color }),
);

const nativeProps = getPartitionedNativeProps({
props,
primarySlotTagName: 'button',
excludedPropNames: ['value', 'color'],
});

const rootVariables = {
[swatchColor]: color,
};

const root = slot.always(props.root, {
defaultProps: {
ref: useFocusWithin<HTMLDivElement>(),
role: props.role ?? 'radio',
'aria-selected': selected,
onClick,
...nativeProps.root,
},
elementType: 'div',
});

const button = slot.always(props.button, {
defaultProps: {
ref,
type: 'button',
...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;
};