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

Svelte 5 #206

Merged
merged 11 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
},
"devDependencies": {
"@eslint/js": "^9.12.0",
"@sveltejs/adapter-auto": "^3.2.5",
"@sveltejs/kit": "^2.7.0",
"@sveltejs/vite-plugin-svelte": "^3.1.2",
"@sveltejs/adapter-auto": "^3.3.0",
"@sveltejs/kit": "^2.7.2",
"@sveltejs/vite-plugin-svelte": "^4.0.0",
"@testing-library/jest-dom": "^6.5.0",
"@testing-library/svelte": "^5.2.3",
"@testing-library/svelte": "^5.2.4",
"@types/eslint-config-prettier": "^6.11.3",
"@types/eslint__js": "^8.42.3",
"@types/lodash": "^4.17.10",
Expand All @@ -59,7 +59,7 @@
"eslint-import-resolver-typescript": "^3.6.3",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-simple-import-sort": "^12.1.1",
"eslint-plugin-svelte": "^2.44.1",
"eslint-plugin-svelte": "^2.45.1",
"jsdom": "^25.0.1",
"lodash": "^4.17.21",
"mockdate": "^3.0.5",
Expand All @@ -72,9 +72,9 @@
"sassdoc-theme-herman": "^6.0.1",
"stylelint": "^16.10.0",
"stylelint-config-standard-scss": "^13.1.0",
"svelte": "^4.2.19",
"svelte": "^5.0.5",
"svelte-check": "^4.0.5",
"svelte-eslint-parser": "^0.41.1",
"svelte-eslint-parser": "^0.43.0",
"typescript": "^5.6.3",
"typescript-eslint": "^8.8.0",
"vite": "^5.4.8",
Expand Down
17 changes: 10 additions & 7 deletions src/lib/components/SpaceSelect.svelte
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
<script lang="ts">
import { to } from 'colorjs.io/fn';
import { run } from 'svelte/legacy';

import { FORMATS } from '$lib/constants';
import { bg, ColorSpace, fg, format } from '$lib/stores';
import { getSpaceFromFormatId } from '$lib/utils';

let spaces: ColorSpace[] = [];
let spaces: ColorSpace[] = $state([]);

$: spaces = FORMATS.map((s) => {
if (s === 'hex') return { id: 'hex', name: 'Hex' } as ColorSpace;
return ColorSpace.get(s);
run(() => {
jamesnw marked this conversation as resolved.
Show resolved Hide resolved
spaces = FORMATS.map((s) => {
if (s === 'hex') return { id: 'hex', name: 'Hex' } as ColorSpace;
return ColorSpace.get(s);
});
});
$: targetSpace = getSpaceFromFormatId($format);
let targetSpace = $derived(getSpaceFromFormatId($format));

// Update color formats when space selection changes
$: {
run(() => {
if ($bg.space.id !== targetSpace) {
$bg = to($bg, targetSpace, { inGamut: true });
}
if ($fg.space.id !== targetSpace) {
$fg = to($fg, targetSpace, { inGamut: true });
}
}
});
</script>

<div data-field="color-format">
Expand Down
12 changes: 8 additions & 4 deletions src/lib/components/colors/FormatGroup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@

import ExternalLink from '../util/ExternalLink.svelte';

export let type: 'bg' | 'fg';
export let color: PlainColorObject;
export let formatGroup: FormatGroup;
interface Props {
type: 'bg' | 'fg';
color: PlainColorObject;
formatGroup: FormatGroup;
}

let { type, color, formatGroup }: Props = $props();

function inGamutForSpace(color: PlainColorObject) {
if (!formatGroup.gamutFormat) return true;
Expand All @@ -19,7 +23,7 @@
);
return inGamut(color, gamutSpace);
}
$: isInGamut = inGamutForSpace(color);
let isInGamut = $derived(inGamutForSpace(color));
</script>

<div data-content="format-group">
Expand Down
14 changes: 9 additions & 5 deletions src/lib/components/colors/Formats.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
type FormatGroup as FormatGroupType,
} from '$lib/constants';

export let type: 'bg' | 'fg';
export let color: PlainColorObject;
export let format: ColorFormatId;
interface Props {
type: 'bg' | 'fg';
color: PlainColorObject;
format: ColorFormatId;
}

let { type, color, format }: Props = $props();

function otherFormatGroups(
selectedFormat: ColorFormatId,
Expand All @@ -25,8 +29,8 @@
return otherFormats;
}

$: displayType = type === 'bg' ? 'Background' : 'Foreground';
$: otherFormats = otherFormatGroups(format);
let displayType = $derived(type === 'bg' ? 'Background' : 'Foreground');
let otherFormats = $derived(otherFormatGroups(format));
</script>

<div data-content="formats" data-column="tool">
Expand Down
42 changes: 24 additions & 18 deletions src/lib/components/colors/Header.svelte
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
<script lang="ts">
import { type PlainColorObject, serialize, to } from 'colorjs.io/fn';
import type { Writable } from 'svelte/store';
import { type Writable } from 'svelte/store';

import CopyButton from '$lib/components/util/CopyButton.svelte';
import type { ColorFormatId } from '$lib/constants';
import { getSpaceFromFormatId } from '$lib/utils';

export let type: 'bg' | 'fg';
export let color: Writable<PlainColorObject>;
export let format: ColorFormatId;
interface Props {
type: 'bg' | 'fg';
color: Writable<PlainColorObject>;
format: ColorFormatId;
}

let { type, color, format }: Props = $props();

$: targetSpace = getSpaceFromFormatId(format);
$: display = serialize($color, { inGamut: false, format });
$: displayType = type === 'bg' ? 'Background' : 'Foreground';
let editing = false;
let inputValue = '';
let hasError = false;
let targetSpace = $derived(getSpaceFromFormatId(format));
let display = $derived(serialize($color, { inGamut: false, format }));
let displayType = $derived(type === 'bg' ? 'Background' : 'Foreground');
let editing = $state(false);
let inputValue = $state('');
let hasError = $state(false);

// When not editing, sync input value with color (e.g. when sliders change)
$: if (!editing) {
inputValue = display;
}
$effect(() => {
if (!editing) {
inputValue = display;
}
});

const handleFocus = () => {
editing = true;
Expand Down Expand Up @@ -71,7 +77,7 @@
data-column="tool"
data-needs-changes={hasError}
>
<div class="swatch {type}" />
<div class="swatch {type}"></div>
<label for="{type}-color" data-label>
{displayType} Color
</label>
Expand All @@ -81,10 +87,10 @@
type="text"
data-input="color"
value={inputValue}
on:focus={handleFocus}
on:blur={handleBlur}
on:input={handleInput}
on:keydown={handleKeydown}
onfocus={handleFocus}
onblur={handleBlur}
oninput={handleInput}
onkeydown={handleKeydown}
/>
<CopyButton text={display} size="medium" />
{#if hasError}
Expand Down
24 changes: 15 additions & 9 deletions src/lib/components/colors/Output.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@
import type { ColorFormatId } from '$lib/constants';
import { getSpaceFromFormatId } from '$lib/utils';

export let type: 'bg' | 'fg';
export let color: PlainColorObject;
export let format: ColorFormatId;
interface Props {
type: 'bg' | 'fg';
color: PlainColorObject;
format: ColorFormatId;
}

let { type, color, format }: Props = $props();

$: targetSpace = getSpaceFromFormatId(format);
$: targetColor = to(color, targetSpace);
$: targetColorValue = serialize(targetColor, {
format,
inGamut: false,
});
let targetSpace = $derived(getSpaceFromFormatId(format));
let targetColor = $derived(to(color, targetSpace));
let targetColorValue = $derived(
serialize(targetColor, {
format,
inGamut: false,
}),
);
</script>

<ul data-group="output {type}">
Expand Down
58 changes: 33 additions & 25 deletions src/lib/components/colors/Sliders.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,39 @@
import { ColorSpace } from '$lib/stores';
import { getSpaceFromFormatId, sliderGradient } from '$lib/utils';

export let type: 'bg' | 'fg';
export let color: Writable<PlainColorObject>;
export let format: ColorFormatId;
interface Props {
type: 'bg' | 'fg';
color: Writable<PlainColorObject>;
format: ColorFormatId;
}

let { type, color, format }: Props = $props();

$: targetSpace = getSpaceFromFormatId(format);
$: spaceObject = ColorSpace.get(targetSpace);
$: sliders = SLIDERS[format].map((id) => {
const coord = spaceObject.coords[id];
const range = coord?.range ?? coord?.refRange ?? [0, 1];
const gradient = sliderGradient($color, id, range);
return {
id,
name: coord?.name ?? '',
range,
index: Number(
ColorSpace.resolveCoord({
space: spaceObject,
coordId: id,
}).index,
),
gradient,
};
});
let targetSpace = $derived(getSpaceFromFormatId(format));
let spaceObject = $derived(ColorSpace.get(targetSpace));
let sliders = $derived(
SLIDERS[format].map((id) => {
const coord = spaceObject.coords[id];
const range = coord?.range ?? coord?.refRange ?? [0, 1];
const gradient = sliderGradient($color, id, range);
return {
id,
name: coord?.name ?? '',
range,
index: Number(
ColorSpace.resolveCoord({
space: spaceObject,
coordId: id,
}).index,
),
gradient,
};
}),
);

$: alphaGradient = sliderGradient($color, 'alpha', [0, $color.alpha]);
let alphaGradient = $derived(
sliderGradient($color, 'alpha', [0, $color.alpha]),
);

const handleInput = (
e: Event & { currentTarget: EventTarget & HTMLInputElement },
Expand Down Expand Up @@ -72,7 +80,7 @@
step={getStep(slider.range)}
style={`--stops: ${slider.gradient}`}
value={$color.coords[slider.index]}
on:input={(e) => handleInput(e, slider.index)}
oninput={(e) => handleInput(e, slider.index)}
/>
</div>
{/each}
Expand All @@ -87,7 +95,7 @@
step={getStep([0, 1])}
style={`--stops: ${alphaGradient}`}
value={$color.alpha}
on:input={(e) => handleInput(e)}
oninput={(e) => handleInput(e)}
/>
</div>
</form>
Expand Down
28 changes: 17 additions & 11 deletions src/lib/components/colors/SupportWarning.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@
import { ColorSpace } from '$lib/stores';
import { getSpaceFromFormatId } from '$lib/utils';

export let format: ColorFormatId;
interface Props {
format: ColorFormatId;
}

let { format }: Props = $props();

$: targetSpace = getSpaceFromFormatId(format);
$: spaceObject = ColorSpace.get(targetSpace);
$: fallbackColor = display(
{
space: spaceObject,
coords: spaceObject.white,
},
{ inGamut: false },
).color;
$: isSupported = fallbackColor.space.id === targetSpace;
let targetSpace = $derived(getSpaceFromFormatId(format));
let spaceObject = $derived(ColorSpace.get(targetSpace));
let fallbackColor = $derived(
display(
{
space: spaceObject,
coords: spaceObject.white,
},
{ inGamut: false },
).color,
);
let isSupported = $derived(fallbackColor.space.id === targetSpace);
</script>

{#if !isSupported}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/colors/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

<SupportWarning format={$format} />

<form data-form="contrast-checker" data-layout="color-form">
<div data-form="contrast-checker" data-layout="color-form">
jamesnw marked this conversation as resolved.
Show resolved Hide resolved
<Header type="bg" color={bg} format={$format} />
<Sliders type="bg" color={bg} format={$format} />

<Header type="fg" color={fg} format={$format} />
<Sliders type="fg" color={fg} format={$format} />
</form>
</div>

<div data-layout="split">
<Formats type="bg" color={$bg} format={$format} />
Expand Down
8 changes: 6 additions & 2 deletions src/lib/components/ratio/ColorIssues.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

import ExternalLink from '$lib/components/util/ExternalLink.svelte';

export let pass: boolean;
interface Props {
pass: boolean;
}

let { pass }: Props = $props();

let startOpen = false;
let startOpen = $state(false);
onMount(() => {
startOpen = window.matchMedia('(min-width: 80em)').matches;
});
Expand Down
12 changes: 8 additions & 4 deletions src/lib/components/ratio/Result.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
import Icon from '$lib/components/util/Icon.svelte';
import { RATIOS } from '$lib/constants';

export let level: 'AA' | 'AAA';
export let type: 'Normal' | 'Large';
export let ratio: number;
interface Props {
level: 'AA' | 'AAA';
type: 'Normal' | 'Large';
ratio: number;
}

let { level, type, ratio }: Props = $props();

$: pass = ratio >= RATIOS[level][type];
let pass = $derived(ratio >= RATIOS[level][type]);
</script>

<div data-pass={pass}>
Expand Down
Loading
Loading