-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
packages/volto-light-theme/src/components/Widgets/ColorPickerWidget.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,71 @@ | ||
import React from 'react'; | ||
import FormFieldWrapper from '@plone/volto/components/manage/Widgets/FormFieldWrapper'; | ||
import { isEmpty, isEqual } from 'lodash'; | ||
import { defineMessages, useIntl } from 'react-intl'; | ||
import { Button } from '@plone/components'; | ||
import cx from 'classnames'; | ||
|
||
const messages = defineMessages({ | ||
Color: { | ||
id: 'Color', | ||
defaultMessage: 'Color', | ||
}, | ||
}); | ||
|
||
type Color = | ||
| { | ||
name: string; | ||
label: string; | ||
style: Record<`--${string}`, string>; | ||
} | ||
| { | ||
name: string; | ||
label: string; | ||
style: undefined; | ||
}; | ||
|
||
export type ColorPickerWidgetProps = { | ||
id: string; | ||
title: string; | ||
value?: string; | ||
default?: string | object; | ||
required?: boolean; | ||
missing_value?: unknown; | ||
className?: string; | ||
onChange: (id: string, value: any) => void; | ||
colors: Color[]; | ||
themes: Color[]; | ||
}; | ||
|
||
const ColorPickerWidget = (props: ColorPickerWidgetProps) => { | ||
const { id, value, onChange } = props; | ||
const colors = props.themes || props.colors || []; | ||
const intl = useIntl(); | ||
|
||
return colors.length > 0 ? ( | ||
<FormFieldWrapper {...props} className="theme-picker-widget"> | ||
<div className="buttons"> | ||
{colors.map((color) => { | ||
const colorName = color.name; | ||
return ( | ||
<Button | ||
key={id + colorName} | ||
className={cx(colorName, { active: value === colorName })} | ||
onPress={(e) => { | ||
onChange( | ||
id, | ||
colorName === value ? props.missing_value : colorName, | ||
); | ||
}} | ||
value={value} | ||
style={color.style} | ||
aria-label={color.label} | ||
/> | ||
); | ||
})} | ||
</div> | ||
</FormFieldWrapper> | ||
) : null; | ||
}; | ||
|
||
export default ColorPickerWidget; |
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