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

Enable color palettes with custom color alpha support #30493

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/components/src/color-palette/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ Whether the palette should have a clearing button or not.
- Required: No
- Default: true

### disableAlpha

Whether the custom color picker allows the selection of alpha values. Only relevant, when
`disableCustomColors` is false. If alpha is enabled, the returned color format will be rgba
instead of hex.

- Type: `Boolean`
- Required: No
- Default: true


## Usage
```jsx
Expand Down
9 changes: 7 additions & 2 deletions packages/components/src/color-palette/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default function ColorPalette( {
disableCustomColors = false,
onChange,
value,
disableAlpha = true,
} ) {
const clearColor = useCallback( () => onChange( undefined ), [ onChange ] );
const colorOptions = useMemo( () => {
Expand Down Expand Up @@ -61,8 +62,12 @@ export default function ColorPalette( {
const renderCustomColorPicker = () => (
<ColorPicker
color={ value }
onChangeComplete={ ( color ) => onChange( color.hex ) }
disableAlpha
onChangeComplete={ ( color ) =>
disableAlpha
? onChange( color.hex )
: onChange( tinycolor( color.rgb ).toRgbString() )
}
disableAlpha={ disableAlpha }
/>
);

Expand Down
13 changes: 13 additions & 0 deletions packages/components/src/color-palette/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,16 @@ export const withKnobs = () => {

return <ColorPaletteWithState colors={ colors } />;
};

export const alphaEnabled = () => {
const colors = [
object( 'Red', { name: 'red transparent', color: '#f00a' } ),
object( 'White', { name: 'white transparent', color: '#fff9' } ),
object( 'Blue', {
name: 'blue transparent',
color: 'rgba(0, 0, 255, 0.5)',
} ),
];

return <ColorPaletteWithState colors={ colors } disableAlpha={ false } />;
};