Skip to content

Commit

Permalink
feat(react-swatch-picker): Added borderColor prop and fixed styles (m…
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinaKozlova authored and miroslavstastny committed Jun 14, 2024
1 parent 9030834 commit 872d6b5
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "feat(react-swatch-picker): added contrast ratio utils",
"packageName": "@fluentui/react-swatch-picker-preview",
"email": "vkozlova@microsoft.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

#### New props

- `borderColor`
- `size`
- `shape`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,14 @@ import { renderSwatchPickerGrid } from '@fluentui/react-swatch-picker-preview';

### ColorSwatch

| Property | Values | Default | Purpose |
| -------- | ---------------------------------------- | -------- | -------------------------------- |
| color | `string` | | Color in hex, RGB or named color |
| shape | `square`, `circular`, `rounded` | `square` | Sets shape |
| size | `extraSmall`, `small`, `medium`, `large` | `medium` | Defines size of the Swatch cell |
| disabled | `boolean` | | |
| value | `string` | | Unique value of the swatch |
| Property | Values | Default | Purpose |
| ----------- | ---------------------------------------- | -------- | -------------------------------- |
| borderColor | `string` | | Sets border color |
| color | `string` | | Color in hex, RGB or named color |
| shape | `square`, `circular`, `rounded` | `square` | Sets shape |
| size | `extraSmall`, `small`, `medium`, `large` | `medium` | Defines size of the Swatch cell |
| disabled | `boolean` | | |
| value | `string` | | Unique value of the swatch |

| Slots | Values | Default | Description |
| ------------ | -------- | -------- | ----------------------------------------------------------------------- |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const colorSwatchClassNames: SlotClassNames<ColorSwatchSlots>;

// @public
export type ColorSwatchProps = ComponentProps<ColorSwatchSlots> & Pick<SwatchPickerProps, 'size' | 'shape'> & {
borderColor?: string;
color: string;
disabled?: boolean;
value: string;
Expand Down Expand Up @@ -101,6 +102,7 @@ export const renderSwatchPickerRow_unstable: (state: SwatchPickerRowState) => JS
// @public (undocumented)
export const swatchCSSVars: {
color: string;
borderColor: string;
};

// @public
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('ColorSwatch', () => {
aria-checked="false"
class="fui-ColorSwatch"
role="radio"
style="--fui-SwatchPicker--color: #f09;"
style="--fui-SwatchPicker--color: #f09; --fui-SwatchPicker--borderColor: var(--colorTransparentStroke);"
type="button"
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export type ColorSwatchSlots = {
*/
export type ColorSwatchProps = ComponentProps<ColorSwatchSlots> &
Pick<SwatchPickerProps, 'size' | 'shape'> & {
/**
* Border color when contrast is low
*/
borderColor?: string;

/**
* Swatch color
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ColorSwatchProps, ColorSwatchState } from './ColorSwatch.types';
import { useSwatchPickerContextValue_unstable } from '../../contexts/swatchPicker';
import { swatchCSSVars } from './useColorSwatchStyles.styles';
import { ProhibitedFilled } from '@fluentui/react-icons';
import { tokens } from '@fluentui/react-theme';

/**
* Create the state required to render ColorSwatch.
Expand All @@ -18,7 +19,7 @@ export const useColorSwatch_unstable = (
props: ColorSwatchProps,
ref: React.Ref<HTMLButtonElement>,
): ColorSwatchState => {
const { color, disabled, disabledIcon, icon, value, onClick, size, shape, style, ...rest } = props;
const { borderColor, color, disabled, disabledIcon, icon, value, onClick, size, shape, style, ...rest } = props;
const _size = useSwatchPickerContextValue_unstable(ctx => ctx.size);
const _shape = useSwatchPickerContextValue_unstable(ctx => ctx.shape);
const isGrid = useSwatchPickerContextValue_unstable(ctx => ctx.isGrid);
Expand All @@ -37,6 +38,7 @@ export const useColorSwatch_unstable = (

const rootVariables = {
[swatchCSSVars.color]: color,
[swatchCSSVars.borderColor]: borderColor ?? tokens.colorTransparentStroke,
};

const role = isGrid ? 'gridcell' : 'radio';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ export const colorSwatchClassNames: SlotClassNames<ColorSwatchSlots> = {

export const swatchCSSVars = {
color: `--fui-SwatchPicker--color`,
borderColor: `--fui-SwatchPicker--borderColor`,
};

const { color } = swatchCSSVars;
const { color, borderColor } = swatchCSSVars;

/**
* Styles for the root slot
Expand All @@ -25,7 +26,7 @@ const useResetStyles = makeResetStyles({
alignItems: 'center',
justifyContent: 'center',
boxSizing: 'border-box',
border: `1px solid ${tokens.colorTransparentStroke}`,
border: `1px solid var(${borderColor})`,
background: `var(${color})`,
overflow: 'hidden',
padding: '0',
Expand Down Expand Up @@ -131,8 +132,10 @@ const useShapeStyles = makeStyles({
const useIconStyles = makeStyles({
disabledIcon: {
color: tokens.colorNeutralForegroundInverted,
filter: 'drop-shadow(0 1px 1px rgb(0 0 0 / 1))',
},
icon: {
position: 'absolute',
display: 'flex',
alignSelf: 'center',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ describe('SwatchPicker', () => {
aria-checked="false"
class="fui-ColorSwatch"
role="radio"
style="--fui-SwatchPicker--color: #f09;"
style="--fui-SwatchPicker--color: #f09; --fui-SwatchPicker--borderColor: var(--colorTransparentStroke);"
type="button"
/>
<button
aria-checked="false"
class="fui-ColorSwatch"
disabled=""
role="radio"
style="--fui-SwatchPicker--color: #0f0;"
style="--fui-SwatchPicker--color: #0f0; --fui-SwatchPicker--borderColor: var(--colorTransparentStroke);"
type="button"
>
<span
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ exports[`Render utils of SwatchPicker renders custom row 1`] = `
aria-selected="false"
class="fui-ColorSwatch"
role="gridcell"
style="--fui-SwatchPicker--color: #FF1921;"
style="--fui-SwatchPicker--color: #FF1921; --fui-SwatchPicker--borderColor: var(--colorTransparentStroke);"
type="button"
/>
<button
aria-label="orange"
aria-selected="false"
class="fui-ColorSwatch"
role="gridcell"
style="--fui-SwatchPicker--color: #FF7A00;"
style="--fui-SwatchPicker--color: #FF7A00; --fui-SwatchPicker--borderColor: var(--colorTransparentStroke);"
type="button"
/>
<button
aria-label="light green"
aria-selected="false"
class="fui-ColorSwatch"
role="gridcell"
style="--fui-SwatchPicker--color: #90D057;"
style="--fui-SwatchPicker--color: #90D057; --fui-SwatchPicker--borderColor: var(--colorTransparentStroke);"
type="button"
/>
</div>
Expand All @@ -46,23 +46,23 @@ exports[`Render utils of SwatchPicker renders custom row 1`] = `
aria-selected="false"
class="fui-ColorSwatch"
role="gridcell"
style="--fui-SwatchPicker--color: #00B053;"
style="--fui-SwatchPicker--color: #00B053; --fui-SwatchPicker--borderColor: var(--colorTransparentStroke);"
type="button"
/>
<button
aria-label="light blue"
aria-selected="false"
class="fui-ColorSwatch"
role="gridcell"
style="--fui-SwatchPicker--color: #00AFED;"
style="--fui-SwatchPicker--color: #00AFED; --fui-SwatchPicker--borderColor: var(--colorTransparentStroke);"
type="button"
/>
<button
aria-label="blue"
aria-selected="false"
class="fui-ColorSwatch"
role="gridcell"
style="--fui-SwatchPicker--color: #006EBD;"
style="--fui-SwatchPicker--color: #006EBD; --fui-SwatchPicker--borderColor: var(--colorTransparentStroke);"
type="button"
/>
</div>
Expand Down Expand Up @@ -113,23 +113,23 @@ exports[`Render utils of SwatchPicker renders custom swatch 1`] = `
aria-selected="false"
class="fui-ColorSwatch custom-swatch"
role="gridcell"
style="--fui-SwatchPicker--color: #FF1921;"
style="--fui-SwatchPicker--color: #FF1921; --fui-SwatchPicker--borderColor: var(--colorTransparentStroke);"
type="button"
/>
<button
aria-label="orange"
aria-selected="false"
class="fui-ColorSwatch custom-swatch"
role="gridcell"
style="--fui-SwatchPicker--color: #FF7A00;"
style="--fui-SwatchPicker--color: #FF7A00; --fui-SwatchPicker--borderColor: var(--colorTransparentStroke);"
type="button"
/>
<button
aria-label="light green"
aria-selected="false"
class="fui-ColorSwatch custom-swatch"
role="gridcell"
style="--fui-SwatchPicker--color: #90D057;"
style="--fui-SwatchPicker--color: #90D057; --fui-SwatchPicker--borderColor: var(--colorTransparentStroke);"
type="button"
/>
</div>
Expand All @@ -142,23 +142,23 @@ exports[`Render utils of SwatchPicker renders custom swatch 1`] = `
aria-selected="false"
class="fui-ColorSwatch custom-swatch"
role="gridcell"
style="--fui-SwatchPicker--color: #00B053;"
style="--fui-SwatchPicker--color: #00B053; --fui-SwatchPicker--borderColor: var(--colorTransparentStroke);"
type="button"
/>
<button
aria-label="light blue"
aria-selected="false"
class="fui-ColorSwatch custom-swatch"
role="gridcell"
style="--fui-SwatchPicker--color: #00AFED;"
style="--fui-SwatchPicker--color: #00AFED; --fui-SwatchPicker--borderColor: var(--colorTransparentStroke);"
type="button"
/>
<button
aria-label="blue"
aria-selected="false"
class="fui-ColorSwatch custom-swatch"
role="gridcell"
style="--fui-SwatchPicker--color: #006EBD;"
style="--fui-SwatchPicker--color: #006EBD; --fui-SwatchPicker--borderColor: var(--colorTransparentStroke);"
type="button"
/>
</div>
Expand All @@ -183,23 +183,23 @@ exports[`Render utils of SwatchPicker renders default grid layout 1`] = `
aria-selected="false"
class="fui-ColorSwatch"
role="gridcell"
style="--fui-SwatchPicker--color: #FF1921;"
style="--fui-SwatchPicker--color: #FF1921; --fui-SwatchPicker--borderColor: var(--colorTransparentStroke);"
type="button"
/>
<button
aria-label="orange"
aria-selected="false"
class="fui-ColorSwatch"
role="gridcell"
style="--fui-SwatchPicker--color: #FF7A00;"
style="--fui-SwatchPicker--color: #FF7A00; --fui-SwatchPicker--borderColor: var(--colorTransparentStroke);"
type="button"
/>
<button
aria-label="light green"
aria-selected="false"
class="fui-ColorSwatch"
role="gridcell"
style="--fui-SwatchPicker--color: #90D057;"
style="--fui-SwatchPicker--color: #90D057; --fui-SwatchPicker--borderColor: var(--colorTransparentStroke);"
type="button"
/>
</div>
Expand All @@ -212,23 +212,23 @@ exports[`Render utils of SwatchPicker renders default grid layout 1`] = `
aria-selected="false"
class="fui-ColorSwatch"
role="gridcell"
style="--fui-SwatchPicker--color: #00B053;"
style="--fui-SwatchPicker--color: #00B053; --fui-SwatchPicker--borderColor: var(--colorTransparentStroke);"
type="button"
/>
<button
aria-label="light blue"
aria-selected="false"
class="fui-ColorSwatch"
role="gridcell"
style="--fui-SwatchPicker--color: #00AFED;"
style="--fui-SwatchPicker--color: #00AFED; --fui-SwatchPicker--borderColor: var(--colorTransparentStroke);"
type="button"
/>
<button
aria-label="blue"
aria-selected="false"
class="fui-ColorSwatch"
role="gridcell"
style="--fui-SwatchPicker--color: #006EBD;"
style="--fui-SwatchPicker--color: #006EBD; --fui-SwatchPicker--borderColor: var(--colorTransparentStroke);"
type="button"
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ export const ColorSwatchVariants = () => {
const styles = useStyles();
return (
<div className={styles.example}>
<ColorSwatch color="purple" value="purple-color" aria-label="Purple" />
<ColorSwatch color="#E3008C" value="hot-pink-color" aria-label="Hot pink" />
<ColorSwatch color="linear-gradient(0deg, #E3008C, #fff232)" value="gradient" aria-label="Gradient yellow pink" />
<ColorSwatch color="#c8eeff" icon={<HeartFilled color="red" />} value="icon" aria-label="heart-icon" />
<ColorSwatch color="#016ab0" disabled value="blue" aria-label="blue" />
<ColorSwatch color="#ff659a" value="initials" aria-label="initials">
A
</ColorSwatch>
<ColorSwatch disabled color="#c8eeff" value="icon" aria-label="heart-icon" />
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Maximum recommended number of swatches per row is 8.
- Use a grid layout when there are more than 4 swatches. The maximum recommended number of swatches in a grid is 64 - an 8 x 8 grid.
- Tooltip should be shown for each color.
- Consider using [Polished](https://polished.js.org/) or [color-parse](https://www.npmjs.com/package/color-parse) library for calculation of contrast ratio.
- When using an icon, make sure that the contrast ratio between the icon and a swatch is a least 3:1.

### Don't
Expand Down

0 comments on commit 872d6b5

Please sign in to comment.