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

Fix color value text overflow effect (fix #20) #21

Open
wants to merge 4 commits into
base: main
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
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

![Color input dropdown](https://raw.githubusercontent.com/cositehq/sanity-plugin-simpler-color-input/main/assets/allow-custom-value.png)


## 🤩 Features

✓ Sanity v3 support
Expand Down Expand Up @@ -57,6 +56,7 @@ export default defineConfig({
{ label: 'Custom...', value: 'custom' },
],
enableSearch: true,
showColorValue: true,
})
],
})
Expand Down Expand Up @@ -141,6 +141,7 @@ const myPortableTextComponents = {
This can be adapted to fit the framework you're using. You just need to know that the `textColor` and `highlightColor` color values are stored in the `value` property.

### A Note on Sanity's Visual Editing Experience

If you are using Sanity's Visual Editing experience, specifically their [overlays](https://www.sanity.io/docs/visual-editing-overlays) and [Stega-encoding](https://www.sanity.io/docs/stega), you will need to clean the color values before using them in your CSS. See how to do this in the discussion [here](https://github.com/cositehq/sanity-plugin-simpler-color-input/issues/11#issuecomment-2341777670).

## ⚙️ Options
Expand Down Expand Up @@ -249,6 +250,22 @@ Which will render accordingly:

This option was removed in favor of the more precise `colorFormat` setting. The alpha slider will be enabled when the `colorFormat` is set to `hexa`, `rgba` or `hsla`.

### Color Value Showing

By default, the library show the color value in the input field. If you want to hide it, you can set `showColorValue` to `false`.

```js
// ...fields...
{
name: 'backgroundColor',
title: 'Background Color with Search',
type: 'simplerColor', // or textColor or highlightColor
options: {
showColorValue: false,
}
}
```

## 📚 Data model

```js
Expand All @@ -275,7 +292,6 @@ with default configuration for build & watch scripts.
See [Testing a plugin in Sanity Studio](https://github.com/sanity-io/plugin-kit#testing-a-plugin-in-sanity-studio)
on how to run this plugin with hotreload in the studio.


### Release new version

Run ["CI & Release" workflow](https://github.com/cositehq/sanity-plugin-simpler-color-input/actions/workflows/main.yml).
Expand Down
33 changes: 25 additions & 8 deletions src/ColorInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface ColorOptions extends Omit<ObjectOptions, 'columns'> {
defaultColorList?: Array<SimplerColorType>
defaultColorFormat?: ColorFormatType
enableSearch?: boolean
showColorValue?: boolean
}

export type SimplerColorSchemaType = Omit<ObjectSchemaType, 'options'> & {
Expand All @@ -45,6 +46,7 @@ export const SimplerColorInput = (props: ObjectInputProps) => {
const {onChange} = props
const value = props.value as SimplerColorType | undefined
const type = props.schemaType as SimplerColorSchemaType
const showColorValue = Boolean(type.options?.showColorValue ?? true)
const [selectedColor, setSelectedColor] = useState<Partial<SimplerColorType> | undefined>(value)

const handleChange = useCallback(
Expand Down Expand Up @@ -156,6 +158,8 @@ export const SimplerColorInput = (props: ObjectInputProps) => {
textAlign: 'center',
borderTopRightRadius: isRequired ? '' : '0',
borderBottomRightRadius: isRequired ? '' : '0',
flexShrink: 1,
overflow: 'hidden',
}}
mode="ghost"
padding={2}
Expand All @@ -165,9 +169,9 @@ export const SimplerColorInput = (props: ObjectInputProps) => {
: setPickerIsOpen(!pickerIsOpen)
}
>
<Inline space={4}>
<Inline space={1}>
<Box>
<Flex style={{width: '100%'}} gap={4}>
<Flex style={{overflow: 'hidden', flexGrow: 1}} align="center" gap={1}>
<Box style={{flexShrink: 0}}>
<Card
style={{backgroundColor: selectedColor?.value || '#ffffff'}}
radius={2}
Expand All @@ -176,11 +180,24 @@ export const SimplerColorInput = (props: ObjectInputProps) => {
margin={1}
/>
</Box>
<Text weight="semibold">{selectedColor?.label || 'Select a color...'} </Text>
<Text>{selectedColor?.value}</Text>
</Inline>
<ChevronDownIcon width={32} height={32} />
</Inline>
<Box
style={{
fontWeight: 600,
overflow: 'hidden',
textOverflow: 'ellipsis',
...(showColorValue && {flexShrink: 0}),
}}
>
{selectedColor?.label || 'Select a color...'}{' '}
</Box>
{showColorValue && (
<Box style={{overflow: 'hidden', textOverflow: 'ellipsis'}}>
{selectedColor?.value}
</Box>
)}
</Flex>
<ChevronDownIcon style={{flexShrink: 0}} width={32} height={32} />
</Flex>
</Button>
{!isRequired && (
<Button
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface SimplerColorInputConfig {
defaultColorList?: Array<{label: string; value: string}>
defaultColorFormat?: ColorFormatType // defaults to 'hex'
enableSearch?: boolean // defaults to false
showColorValue?: boolean // default to true
}

/**
Expand Down