Skip to content

Commit

Permalink
Show color hex on hover for color picker
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyGuthridge committed Jan 28, 2025
1 parent 0b300a7 commit 8b4b308
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
30 changes: 18 additions & 12 deletions src/components/base/Button.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
mode?: 'default' | 'warning' | 'confirm';
/** Hint for button (controls tooltip and aria-label) */
hint?: string;
/** Whether the hint tool-tip should be interactive */
hintInteractive?: boolean;
// Standard button props
children: Snippet;
Expand All @@ -20,6 +22,7 @@
const {
children,
hint,
hintInteractive = false,
type,
disabled,
onclick,
Expand Down Expand Up @@ -51,18 +54,21 @@
</script>

{#if hint}
<button
{onclick}
{type}
{disabled}
use:tooltip={{ content: hint }}
aria-label={hint}
style:--color={color}
style:--hoverColor={hoverColor}
style:--clickColor={clickColor}
>
{@render children()}
</button>
<!-- https://atomiks.github.io/tippyjs/v6/accessibility/#interactivity -->
<div>
<button
{onclick}
{type}
{disabled}
use:tooltip={{ content: hint, interactive: hintInteractive }}
aria-label={hint}
style:--color={color}
style:--hoverColor={hoverColor}
style:--clickColor={clickColor}
>
{@render children()}
</button>
</div>
{:else}
<button
{onclick}
Expand Down
5 changes: 3 additions & 2 deletions src/components/pickers/ColorPicker.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import { Button } from '$components/base';
import { colorName } from '$lib/color';
import { colord } from 'colord';
type Props = {
value: string;
Expand All @@ -10,13 +11,13 @@
let { value = $bindable(), oninput, required }: Props = $props();
const name = $derived(colorName(value));
const name = $derived(colorName(colord(value)));
let picker: HTMLInputElement;
</script>

<div>
<Button onclick={() => picker.click()}>
<Button onclick={() => picker.click()} hint={value} hintInteractive>
<div class="button-wrap">
<span>{name}</span>
<div class="color-preview" style:--color={value}></div>
Expand Down
2 changes: 1 addition & 1 deletion src/hooks.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import consts from '$lib/consts';
function helloWorld() {
console.log(`%c${consts.APP_NAME}\n`, 'font-size: 3rem');
console.log(`%cMade with <3 by ${consts.APP_AUTHOR[0]}`, 'font-size: 1.5rem');
console.log(`Check out the code on\n${consts.APP_GITHUB}`);
console.log(`Check out the code at\n${consts.APP_GITHUB}`);
}

export const init = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function scaleLightness(color: Colord, factor: number): Colord {

/** Return the name of the given color */
export function colorName(color: Colord): string {
const rawName = colord(color).toName({ closest: true })!;
const rawName = color.toName({ closest: true })!;

const suffixes: (string | [string, string])[] = [
['seagreen', 'sea-green'],
Expand Down

0 comments on commit 8b4b308

Please sign in to comment.