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 6 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 @@ -156,6 +156,8 @@ export const CustomStyleHooksContext_unstable: React_2.Context<Partial<{
useTeachingPopoverSurfaceStyles_unstable: CustomStyleHook;
useTeachingPopoverTitleStyles_unstable: CustomStyleHook;
useTimePickerCompatStyles_unstable: CustomStyleHook;
useSwatchPickerStyles_unstable: CustomStyleHook;
useColorSwatchStyles_unstable: CustomStyleHook;
}> | undefined>;

// @public (undocumented)
Expand Down Expand Up @@ -281,6 +283,8 @@ export type CustomStyleHooksContextValue_unstable = Partial<{
useTeachingPopoverSurfaceStyles_unstable: CustomStyleHook;
useTeachingPopoverTitleStyles_unstable: CustomStyleHook;
useTimePickerCompatStyles_unstable: CustomStyleHook;
useSwatchPickerStyles_unstable: CustomStyleHook;
useColorSwatchStyles_unstable: CustomStyleHook;
}>;

// @internal (undocumented)
Expand Down Expand Up @@ -406,6 +410,8 @@ export const CustomStyleHooksProvider_unstable: React_2.Provider<Partial<{
useTeachingPopoverSurfaceStyles_unstable: CustomStyleHook;
useTeachingPopoverTitleStyles_unstable: CustomStyleHook;
useTimePickerCompatStyles_unstable: CustomStyleHook;
useSwatchPickerStyles_unstable: CustomStyleHook;
useColorSwatchStyles_unstable: CustomStyleHook;
}> | undefined>;

// @internal (undocumented)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ export type CustomStyleHooksContextValue = Partial<{
useTeachingPopoverSurfaceStyles_unstable: CustomStyleHook;
useTeachingPopoverTitleStyles_unstable: CustomStyleHook;
useTimePickerCompatStyles_unstable: CustomStyleHook;
useSwatchPickerStyles_unstable: CustomStyleHook;
useColorSwatchStyles_unstable: CustomStyleHook;
ValentinaKozlova marked this conversation as resolved.
Show resolved Hide resolved
}>;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,87 @@ 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 = ComponentProps<ColorSwatchSlots> & {
color: string;
size?: 'extraSmall' | 'small' | 'medium' | 'large';
shape?: 'rounded' | 'square' | 'circular';
value: string;
};

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

// @public
export type ColorSwatchState = ComponentState<ColorSwatchSlots> & Pick<ColorSwatchProps, 'color' | 'size' | 'shape' | 'value'> & SwatchContextValue;

// @public
export const renderColorSwatch_unstable: (state: ColorSwatchState, contextValues: SwatchContextValues) => 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;
};

// @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 @@ -32,6 +32,8 @@
},
"dependencies": {
"@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,46 @@
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',
testOptions: {
'has-static-classnames': [
{
props: {},
expectedClassNames: {
root: colorSwatchClassNames.root,
},
},
],
},
});

// TODO add more tests here, and create visual regression tests in /apps/vr-tests
ValentinaKozlova marked this conversation as resolved.
Show resolved Hide resolved

it('renders a default state', () => {
const result = render(
<ColorSwatch color="#f09" value="#f09">
Default ColorSwatch
</ColorSwatch>,
);
expect(result.container).toMatchInlineSnapshot(`
<div>
<button
aria-selected="false"
class="fui-ColorSwatch"
role="radio"
style="--fui-SwatchPicker--color: #f09;"
tabindex="0"
value="#f09"
>
Default ColorSwatch
</button>
</div>
`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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';
import { useSwatchContextValues_unstable } from '../../contexts/swatch';

/**
* ColorSwatch component - TODO: add more docs
*/
export const ColorSwatch: ForwardRefComponent<ColorSwatchProps> = React.forwardRef((props, ref) => {
const state = useColorSwatch_unstable(props, ref);
const contextValues = useSwatchContextValues_unstable(state);

useColorSwatchStyles_unstable(state);
useCustomStyleHook_unstable('useColorSwatchStyles_unstable')(state);

return renderColorSwatch_unstable(state, contextValues);
});

ColorSwatch.displayName = 'ColorSwatch';
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { ComponentProps, ComponentState, Slot } from '@fluentui/react-utilities';
import { SwatchContextValue } from '../../contexts/swatch';

export type ColorSwatchSlots = {
root: Slot<'button'>;
};

/**
* ColorSwatch Props
*/
export type ColorSwatchProps = ComponentProps<ColorSwatchSlots> & {
/**
* Swatch color
*/
color: string;

/**
* Swatch size
* @defaultvalue 'medium'
*/
size?: 'extraSmall' | 'small' | 'medium' | 'large';

/**
* Swatch shape
* @defaultvalue 'square'
*/
shape?: 'rounded' | 'square' | 'circular';
/**
* Swatch value
*/
value: string;
};

/**
* State used in rendering ColorSwatch
*/
export type ColorSwatchState = ComponentState<ColorSwatchSlots> &
Pick<ColorSwatchProps, 'color' | 'size' | 'shape' | 'value'> &
SwatchContextValue;
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';
import { SwatchContextValues, SwatchProvider } from '../../contexts/swatch';

/**
* Render the final JSX of ColorSwatch
*/
export const renderColorSwatch_unstable = (state: ColorSwatchState, contextValues: SwatchContextValues) => {
assertSlots<ColorSwatchSlots>(state);
return (
<SwatchProvider value={contextValues.swatch}>
<state.root />
</SwatchProvider>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import * as React from 'react';
import { getIntrinsicElementProps, slot, useMergedRefs, useEventCallback } from '@fluentui/react-utilities';
import type { ColorSwatchProps, ColorSwatchState } from './ColorSwatch.types';
import { SwatchPickerOnSelectionChangeEvent } from '../SwatchPicker/SwatchPicker.types';
import { useColorSwatchState_unstable } from './useColorSwatchState';
import { useFocusWithin } from '@fluentui/react-tabster';
import { useSwatchPickerContextValue_unstable } from '../../contexts/swatchPicker';

/**
* 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 HTMLDivElement of ColorSwatch
*/
export const useColorSwatch_unstable = (
props: ColorSwatchProps,
ref: React.Ref<HTMLButtonElement>,
): ColorSwatchState => {
const { color, value } = props;

const context = useSwatchPickerContextValue_unstable();
const requestSelectionChange = context.requestSelectionChange;
const selected = context.selectedValue === value;
ValentinaKozlova marked this conversation as resolved.
Show resolved Hide resolved
const onClick = useEventCallback((event: SwatchPickerOnSelectionChangeEvent) =>
requestSelectionChange({ event, selectedValue: value }),
);

const _role = 'radio';

const state: ColorSwatchState = {
components: {
root: 'button',
},
root: slot.always(
getIntrinsicElementProps('button', {
ref,
...props,
role: props.role ?? _role,
ValentinaKozlova marked this conversation as resolved.
Show resolved Hide resolved
tabIndex: 0,
'aria-selected': selected,
onClick,
}),
{ elementType: 'button' },
),
size: context.size,
shape: context.shape,
selected,
color,
value,
};

state.root.ref = useMergedRefs(state.root.ref, useFocusWithin<HTMLButtonElement>());
ValentinaKozlova marked this conversation as resolved.
Show resolved Hide resolved

useColorSwatchState_unstable(state, props);
ValentinaKozlova marked this conversation as resolved.
Show resolved Hide resolved
return state;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// import * as React from 'react';
ValentinaKozlova marked this conversation as resolved.
Show resolved Hide resolved
import { swatchCSSVars } from './useColorSwatchStyles.styles';
import type { ColorSwatchProps, ColorSwatchState } from './ColorSwatch.types';

const { swatchColor } = swatchCSSVars;

export const useColorSwatchState_unstable = (state: ColorSwatchState, props: ColorSwatchProps) => {
const { color = '#000' } = props;
ValentinaKozlova marked this conversation as resolved.
Show resolved Hide resolved

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

// Root props
state.root.style = {
...rootVariables,
ValentinaKozlova marked this conversation as resolved.
Show resolved Hide resolved
...state.root.style,
};
};