Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
a9b7498
Basic support for the editable prop
DmitrySharabin Nov 20, 2024
efcf55c
Allow add colors
DmitrySharabin Nov 20, 2024
5a1998a
Allow remove colors
DmitrySharabin Nov 20, 2024
283445e
Improve UX
DmitrySharabin Nov 20, 2024
f08d70c
Add an example
DmitrySharabin Nov 20, 2024
e5668ec
Convert the new color to the current color space
DmitrySharabin Nov 20, 2024
a2d93eb
Remove extra braces
DmitrySharabin Nov 20, 2024
240a430
Some may define boolean attributes using their name as the value
DmitrySharabin Nov 20, 2024
163cb1e
Address feedback
DmitrySharabin Nov 20, 2024
15ab8fd
Get rid of the wrong actions
DmitrySharabin Nov 20, 2024
3eb0990
Add comment
DmitrySharabin Nov 21, 2024
83f52cc
Intermediate colors should not be editable
DmitrySharabin Nov 21, 2024
859ae6e
Allow to update base colors
DmitrySharabin Nov 21, 2024
235f8c4
Allow specifying (optional) color and name when adding a new color
DmitrySharabin Nov 21, 2024
54aa230
Allow to update names of base colors
DmitrySharabin Nov 21, 2024
8494751
Improve warnings
DmitrySharabin Nov 21, 2024
d8519db
Make the `if` condition stricter
DmitrySharabin Nov 21, 2024
f7d3a26
Allow to remove base colors
DmitrySharabin Nov 21, 2024
2e575a0
Don't set the swatch color too early
DmitrySharabin Nov 21, 2024
bee1d30
We don't need to add a new part; the remove color button can be style…
DmitrySharabin Nov 21, 2024
47fc7f4
Style the remove color button
DmitrySharabin Nov 21, 2024
0245eaa
Add some more styles
DmitrySharabin Nov 21, 2024
d2f2f99
Add some more examples
DmitrySharabin Nov 21, 2024
c99b2bc
More docs
DmitrySharabin Nov 21, 2024
d5056e2
Add FIXMEs
DmitrySharabin Nov 21, 2024
81505cd
Update selectors
DmitrySharabin Nov 21, 2024
ed3ff16
Some code tweaks
DmitrySharabin Nov 22, 2024
2dc312a
Tweak the delete button
DmitrySharabin Nov 22, 2024
4e3489d
Fix regression
DmitrySharabin Nov 22, 2024
66a8ff0
Address feedback: Show simple cases first, then complex ones
DmitrySharabin Dec 3, 2024
0fbd31d
Don't capture `input` events — they bubble
DmitrySharabin Dec 3, 2024
15d24dc
Adopt new `<color-swatch>` API: Use the `label` prop
DmitrySharabin Dec 3, 2024
e5276d3
Don't mess up with the named slots
DmitrySharabin Dec 3, 2024
04b6660
Adjust the delete button styles
DmitrySharabin Dec 3, 2024
cd9cabf
Rename `editable` → `edit`
DmitrySharabin Dec 3, 2024
0c2cda9
First step in adding support for list operations
DmitrySharabin Dec 3, 2024
ea3b5f8
Re-style the Add color button
DmitrySharabin Dec 3, 2024
fad2b21
Use classes to hide action buttons
DmitrySharabin Dec 3, 2024
ae0c993
Merge branch 'main' into editable-attribute
DmitrySharabin Dec 4, 2024
5c99a16
Revert to `editable`
DmitrySharabin Dec 5, 2024
74b2f43
Add/remove the `Add color` button dynamically
DmitrySharabin Dec 5, 2024
a4f99f7
Add/remove `Delete color` buttons dynamically
DmitrySharabin Dec 5, 2024
3e1d791
Make the `Delete button` a part
DmitrySharabin Dec 5, 2024
710894a
[color-swatch] Add the `editable` prop and `labelchange` event
DmitrySharabin Dec 5, 2024
a9cc330
Refactor `updateColorName()`
DmitrySharabin Dec 5, 2024
d856b4f
Refactor `updateColor()`
DmitrySharabin Dec 5, 2024
d88323c
Refactor `deleteColor()`
DmitrySharabin Dec 5, 2024
228472f
[color-swatch] Make the color of a static swatch editable
DmitrySharabin Dec 5, 2024
114fdf6
Fix regression with `addColor()`
DmitrySharabin Dec 5, 2024
9951c34
Fix tab order
DmitrySharabin Dec 5, 2024
3f5e90d
Improve docs
DmitrySharabin Dec 5, 2024
cd6eb65
Correctly remove colors in scales with interpolation
DmitrySharabin Dec 5, 2024
cf3c4c0
Address feedback: Use separate parts for action buttons
DmitrySharabin Dec 11, 2024
d0eee10
Fix some issues with styles in Safari
DmitrySharabin Dec 11, 2024
d26f932
Address feedback: Add the icon source and remove the namespace (we do…
DmitrySharabin Dec 12, 2024
84d27d2
Address feedback: Use a middle gray as the default color
DmitrySharabin Dec 12, 2024
6076669
Address feedback: Make `defaultColor` a JS prop, not a function
DmitrySharabin Dec 12, 2024
aea75f6
Address feedback: Fix layout issues on small devices
DmitrySharabin Dec 12, 2024
71000c4
Address feedback: Make the label edit UI less prominent
DmitrySharabin Dec 12, 2024
0e6276d
Address feedback: Use `document.activeElement`
DmitrySharabin Dec 12, 2024
16f0794
Fix bug when multiple `input`s could be created
DmitrySharabin Dec 12, 2024
49fb4d6
Don't render already rendered scales
DmitrySharabin Dec 12, 2024
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
92 changes: 92 additions & 0 deletions src/color-scale/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,98 @@ You can also create compact color scales, by simply setting `--details-style: co

Issue: How to make them focusable??

### The `editable` attribute

The `editable` attribute allows you to make the color scale editable: adding new colors and editing, reordering, or removing existing ones.

Use `editable` as a boolean attribute to make the color scale editable:

```html
<color-scale editable space="oklch" colors="
Gray 300: #d1d5db,
Gray 400: #9ca3af,
Gray 500: #6b7280
"></color-scale>
```

You can specify whether the color names, color values, or both should be editable.

Editing colors:

```html
<color-scale editable="color" space="oklch" colors="Peach: #f6d6d6, Yellow: #f6f7c4, Mint: #a1eebd, Blue: #7bd3ea"></color-scale>
```

Editing color names:

```html
<color-scale editable="name" space="oklch" colors="Peach: #f6d6d6, Yellow: #f6f7c4, Mint: #a1eebd, Blue: #7bd3ea"></color-scale>
```

Or both:

```html
<color-scale editable="name color" space="oklch" colors="Peach: #f6d6d6, Yellow: #f6f7c4, Mint: #a1eebd, Blue: #7bd3ea"></color-scale>
```

Add `list` to enable all list operations (add, delete, reorder) in one go:

```html
<color-scale editable="name color list" space="oklch" colors="Peach: #f6d6d6, Yellow: #f6f7c4, Mint: #a1eebd, Blue: #7bd3ea"></color-scale>
```

For more granular control, specify operations to be enabled:

```html
<color-scale editable="add reorder" space="oklch" colors="#c5f6fa, #99e9f2, #3bc9db"></color-scale>
```

Keep in mind that interpolated colors are generated automatically, are not editable, and cannot be reordered or deleted:

```html
<color-scale editable="color reorder delete" colors="Peach: #f6d6d6, Yellow: #f6f7c4, Mint: #a1eebd" steps="1" space="oklch"></color-scale>
```

The `editable` attribute is reactive and can be set programmatically:

```html
<label>
<input type="checkbox" onchange="this.parentElement.nextElementSibling.editable = this.checked">Editable
</label>
<color-scale space="oklch" colors="Peach: #f6d6d6, Yellow: #f6f7c4, Mint: #a1eebd"></color-scale>
```

You can also add colors programmatically with the `addColor()` method, providing the color to add and, optionally, its name:

```html
<button onclick="this.nextElementSibling.addColor('#7bd3ea', 'Blue')">Add blue color</button>
<color-scale space="oklch" colors="Peach: #f6d6d6, Yellow: #f6f7c4, Mint: #a1eebd"></color-scale>
```

Or don't provide any of them. In that case, the added swatch will get the color of the last one:

```html
<button onclick="this.nextElementSibling.addColor()">Add default color</button>
<color-scale space="oklch" colors="Peach: #f6d6d6, Yellow: #f6f7c4, Mint: #a1eebd"></color-scale>
```

Don't want added swatches to get the color of the last one? No problem. You can change that, too.
Set the `defaultColor` property of the color scale to the desired color. If the value of the property is a color object,
you can give the color a name by adding the `name` property to it:

```html
<button onclick="this.nextElementSibling.addColor()">Add my color</button>
<color-scale id="custom_color" space="oklch" colors="Peach: #f6d6d6, Yellow: #f6f7c4, Mint: #a1eebd"></color-scale>

<script type="module">
import Color from "https://colorjs.io/dist/color.js";

let color = new Color("#f06");
color.name = "My awesome color";
custom_color.defaultColor = color;
</script>
```

<!--
If you want to insert interpolated colors only in specific places, you can use empty values:

Expand Down
90 changes: 79 additions & 11 deletions src/color-scale/color-scale.css
Original file line number Diff line number Diff line change
@@ -1,26 +1,94 @@
:host {
[part="wrapper"] {
display: grid;
gap: .3em;
grid-auto-flow: row;
grid-template-rows: auto auto;
grid-template-columns: repeat(auto-fit, minmax(0, 1fr));
}

#swatches {
display: flex;
gap: inherit;
display: contents;
}

[part~="button"] {
/* Common styles */
all: unset;

display: grid;
place-items: center;

color: var(--_foreground-color, canvastext);
background-color: var(--_background-color, canvas);

border: 3px solid color-mix(in oklab, oklab(50% 0 0) 20%, oklab(none none none / 0%));
border-radius: 50%;

cursor: pointer;

&:is(:hover, :focus) {
color: var(--_background-color, canvas);
background-color: var(--_foreground-color, canvastext);
}
}

color-swatch {
position: relative;
margin: 0;
flex: 1;

&:not(:hover, :focus-within) {
[part="delete button"] {
opacity: 0;
transition-duration: 0ms;
}
}

&::part(color) {
flex-direction: column;
}

&::part(label) {
min-inline-size: initial;

word-break: break-all;
overscroll-behavior: contain;
}

input {
min-inline-size: initial;
}

[part="delete button"] {
--_foreground-color: red;

position: absolute;
inset-block-start: .3em;
inset-inline-end: .3em;

inline-size: 1.2em;
padding: .2em;

box-shadow: 0 .05em 1em -.7em black;

transition: opacity .2s;
}
}

[part="add button"] {
--_foreground-color: deeppink;

padding: .18em;

border-style: dashed;
border-radius: .2rem;

font: inherit;
font-size: 200%;
font-weight: bold;
}

@supports (grid-template-columns: subgrid) {
/* Avoid uneven swatch heights */
#swatches {
display: grid;
grid-auto-flow: row;
grid-template-rows: auto auto;
grid-template-columns: repeat(auto-fit, minmax(0, 1fr));
}

color-swatch {
width: 100%;
grid-row: 1 / span 2;
Expand All @@ -37,4 +105,4 @@ color-swatch {
grid-row: 1;
}
}
}
}
Loading