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

feat(reactive-controllers): Migrate to Colorjs from Tinycolor #4713

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
cfeaf31
feat(reactive-controllers): added color orchestration layer
blunteshwar Sep 2, 2024
b9bb98a
chore: updated yarn lock
blunteshwar Sep 2, 2024
b68e457
chore(reactive-controllers): minor fix
blunteshwar Sep 2, 2024
6b6528f
chore: brought upto main
blunteshwar Sep 4, 2024
882166c
chore: updated yarn lock
blunteshwar Sep 4, 2024
c914192
Merge branch 'main' into color-migration
blunteshwar Sep 17, 2024
39e026f
chore(reactive-controllers): added tests and color formats
blunteshwar Sep 19, 2024
b73eba7
chore(reactive-controllers): updated rgb limits
blunteshwar Sep 19, 2024
f54193c
chore: brought upto main
blunteshwar Sep 19, 2024
92a16f7
chore(color-field): updated color field
blunteshwar Sep 29, 2024
8034f31
Merge branch 'main' into color-migration
blunteshwar Sep 29, 2024
a89238d
chore(color-field): updated tests
blunteshwar Sep 30, 2024
819f259
chore(reactive-controllers): added some tests
blunteshwar Sep 30, 2024
88c555b
chore(reactive-controllers): added some more tests
blunteshwar Oct 7, 2024
538e82f
Merge branch 'main' into color-migration
blunteshwar Oct 7, 2024
b5f307e
Merge branch 'main' into color-migration
blunteshwar Oct 14, 2024
a7d5257
chore(color-area, color-slider): migrated to colorjs
blunteshwar Oct 17, 2024
c39f021
Merge branch 'color-migration' of github.com:adobe/spectrum-web-compo…
blunteshwar Oct 17, 2024
6ffc250
chore(color-slider): updated tests
blunteshwar Oct 17, 2024
e7d473f
chore(color-area): resolved tests
blunteshwar Oct 17, 2024
2c9e7b1
chore(color-wheel): migrated color wheel to colorjs
blunteshwar Oct 17, 2024
17e3ab1
chore(color-area): updated stories
blunteshwar Oct 17, 2024
a81399a
Merge branch 'main' into color-migration
blunteshwar Oct 17, 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
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ executors:
parameters:
current_golden_images_hash:
type: string
default: 3bb8a93772e35ef3bb5e156f76b7c8bd72786144
default: e7d473fdc54f656c3cc077b7c99d6c1cd8c4a8de
wireit_cache_name:
type: string
default: wireit
Expand Down
73 changes: 34 additions & 39 deletions packages/color-area/src/ColorArea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ import { streamingListener } from '@spectrum-web-components/base/src/streaming-l
import { SWCResizeObserverEntry, WithSWCResizeObserver } from './types';
import type { ColorHandle } from '@spectrum-web-components/color-handle';
import '@spectrum-web-components/color-handle/sp-color-handle.js';

import {
ColorController,
ColorValue,
} from '@spectrum-web-components/reactive-controllers/src/Color.js';
ColorTypes,
} from '@spectrum-web-components/reactive-controllers/src/ColorController.js';
import { LanguageResolutionController } from '@spectrum-web-components/reactive-controllers/src/LanguageResolution.js';
import {
isAndroid,
Expand Down Expand Up @@ -72,18 +73,7 @@ export class ColorArea extends SpectrumElement {

private languageResolver = new LanguageResolutionController(this);

private colorController = new ColorController(this, {
extractColorFromState: () => ({
h: this.hue,
s: this.x,
v: this.y,
}),
applyColorToState: ({ s, v }) => {
this._x = s;
this._y = v;
this.requestUpdate();
},
});
private colorController = new ColorController(this, { manageAs: 'hsv' });

@property({ type: Number })
public get hue(): number {
Expand All @@ -95,16 +85,16 @@ export class ColorArea extends SpectrumElement {
}

@property({ type: String })
public get value(): ColorValue {
return this.colorController.color;
public get value(): ColorTypes {
return this.colorController.colorValue;
}

@property({ type: String })
public get color(): ColorValue {
return this.colorController.color;
public get color(): ColorTypes {
return this.colorController.colorValue;
}

public set color(color: ColorValue) {
public set color(color: ColorTypes) {
this.colorController.color = color;
}

Expand All @@ -113,48 +103,48 @@ export class ColorArea extends SpectrumElement {

@property({ type: Number })
public get x(): number {
return this._x;
return this.colorController.color.hsv.s / 100;
}

public set x(x: number) {
if (x === this.x) {
return;
}
const oldValue = this.x;
this._x = x;
if (this.inputX) {
// Use the native `input[type='range']` control to validate this value after `firstUpdate`
this.inputX.value = x.toString();
this._x = this.inputX.valueAsNumber;
this.colorController.color.set(
's',
this.inputX.valueAsNumber * 100
);
} else {
this.colorController.color.set('s', x * 100);
}
this.requestUpdate('x', oldValue);
this.colorController.applyColorFromState();
}

private _x = 1;

@property({ type: Number })
public get y(): number {
return this._y;
return this.colorController.color.hsv.v / 100;
}

public set y(y: number) {
if (y === this.y) {
return;
}
const oldValue = this.y;
this._y = y;
if (this.inputY) {
// Use the native `input[type='range']` control to validate this value after `firstUpdate`
this.inputY.value = y.toString();
this._y = this.inputY.valueAsNumber;
this.colorController.color.set(
'v',
this.inputY.valueAsNumber * 100
);
}
this.requestUpdate('y', oldValue);
this.colorController.applyColorFromState();
}

private _y = 1;

@property({ type: Number })
public step = 0.01;

Expand All @@ -164,7 +154,7 @@ export class ColorArea extends SpectrumElement {
@query('[name="y"]')
public inputY!: HTMLInputElement;

private altered = 0;
public altered = 0;

private activeKeys = new Set();

Expand Down Expand Up @@ -201,6 +191,7 @@ export class ColorArea extends SpectrumElement {
private handleKeydown(event: KeyboardEvent): void {
const { code } = event;
this.focused = true;

this.altered = [event.shiftKey, event.ctrlKey, event.altKey].filter(
(key) => !!key
).length;
Expand Down Expand Up @@ -262,7 +253,6 @@ export class ColorArea extends SpectrumElement {
this.y = Math.min(1, Math.max(this.y + deltaY, 0));

this.colorController.savePreviousColor();
this.colorController.applyColorFromState();

if (deltaX != 0 || deltaY != 0) {
this._valueChanged = true;
Expand Down Expand Up @@ -295,7 +285,6 @@ export class ColorArea extends SpectrumElement {
const { valueAsNumber, name } = event.target;

this[name as 'x' | 'y'] = valueAsNumber;
this.colorController.applyColorFromState();
}

private handleChange(event: Event & { target: HTMLInputElement }): void {
Expand Down Expand Up @@ -332,8 +321,8 @@ export class ColorArea extends SpectrumElement {
this._valueChanged = false;

this.x = x;
this.y = 1 - y;
this.colorController.applyColorFromState();
this.y = y;

this.dispatchEvent(
new Event('input', {
bubbles: true,
Expand Down Expand Up @@ -390,7 +379,7 @@ export class ColorArea extends SpectrumElement {
Math.min(1, (offsetY - minOffsetY) / height)
);

return [this.isLTR ? percentX : 1 - percentX, percentY];
return [this.isLTR ? percentX : 1 - percentX, 1 - percentY];
}

private handleAreaPointerdown(event: PointerEvent): void {
Expand Down Expand Up @@ -537,10 +526,16 @@ export class ColorArea extends SpectrumElement {
protected override updated(changed: PropertyValues): void {
super.updated(changed);
if (this.x !== this.inputX.valueAsNumber) {
this._x = this.inputX.valueAsNumber;
this.colorController.color.set(
's',
this.inputX.valueAsNumber * 100
);
}
if (this.y !== this.inputY.valueAsNumber) {
this._y = this.inputY.valueAsNumber;
this.colorController.color.set(
'v',
(1 - this.inputY.valueAsNumber) * 100
);
}
if (changed.has('focused') && this.focused) {
// Lazily bind the `input[type="range"]` elements in shadow roots
Expand Down
15 changes: 13 additions & 2 deletions packages/color-area/stories/color-area.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export default {
onChange: { action: 'change' },
color: {
name: 'color',
type: { name: 'ColorValue', required: 'true' },
type: { name: 'ColorTypes', required: 'true' },
description: 'The color displayed by the ColorArea.',
table: {
type: { summary: 'ColorValue' },
type: { summary: 'ColorTypes' },
defaultValue: { summary: '' },
},
control: 'text',
Expand All @@ -44,6 +44,7 @@ type StoryArgs = {
export const Default = ({ onChange, onInput }: StoryArgs): TemplateResult => {
return html`
<sp-color-area
color="#ff0000"
@input=${({ target }: Event & { target: ColorArea }) => {
const next = target.nextElementSibling as HTMLElement;
next.textContent = target.color as string;
Expand All @@ -58,6 +59,16 @@ export const Default = ({ onChange, onInput }: StoryArgs): TemplateResult => {
`;
};

export const appliedValues = (): TemplateResult => {
return html`
<sp-color-area
.color=${{ space: 'hsv', coords: [250, 90, 80] }}
></sp-color-area>
<sp-color-area color="hsv(250, 90%, 80%)"></sp-color-area>
<sp-color-area hue="250" x="0.1" y="0.1"></sp-color-area>
`;
};

export const joint = (): TemplateResult => {
return html`
<div>
Expand Down
Loading
Loading