Skip to content

Commit ff839b8

Browse files
fix: broken Clear button for textColor and highlightColor annotations
1 parent 04786f0 commit ff839b8

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sanity-plugin-simpler-color-input",
3-
"version": "1.3.0",
3+
"version": "1.3.1",
44
"description": "A simpler color input",
55
"keywords": [
66
"sanity",

src/ColorInput.tsx

+13-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import styled from 'styled-components'
99
export interface SimplerColorType {
1010
label: string
1111
value: string
12+
_type?: string // included in textColor and highlightColor annotation types
13+
_key?: string // included in textColor and highlightColor annotation types
1214
}
1315

1416
export interface ColorOptions extends Omit<ObjectOptions, 'columns'> {
@@ -29,7 +31,7 @@ export const SimplerColorInput = (props: ObjectInputProps) => {
2931
const {onChange} = props
3032
const value = props.value as SimplerColorType | undefined
3133
const type = props.schemaType as SimplerColorSchemaType
32-
const [selectedColor, setSelectedColor] = useState<SimplerColorType | undefined>(value)
34+
const [selectedColor, setSelectedColor] = useState<Partial<SimplerColorType> | undefined>(value)
3335

3436
const handleChange = useCallback(
3537
(color: SimplerColorType) => {
@@ -142,8 +144,16 @@ export const SimplerColorInput = (props: ObjectInputProps) => {
142144
<Button
143145
mode="ghost"
144146
onClick={() => {
145-
setSelectedColor(undefined)
146-
onChange(unset())
147+
if (value !== undefined && value._key) {
148+
// we need to handle annotations differently to
149+
// prevent errors in the Portable Text editor
150+
const annotationValue = {_type: value._type, _key: value._key}
151+
setSelectedColor(annotationValue)
152+
onChange(set(annotationValue))
153+
} else {
154+
setSelectedColor(undefined)
155+
onChange(unset())
156+
}
147157
}}
148158
style={{borderTopLeftRadius: '0', borderBottomLeftRadius: '0'}}
149159
>

0 commit comments

Comments
 (0)