anki/ts/components/ColorPicker.svelte

79 lines
2.0 KiB
Svelte
Raw Normal View History

2021-04-15 15:59:52 +02:00
<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script lang="typescript">
import { onMount, createEventDispatcher, getContext } from "svelte";
import { nightModeKey } from "./contextKeys";
import { mergeTooltipAndShortcut } from "./helpers";
export let id: string;
export let className = "";
export let tooltip: string | undefined;
export let shortcutLabel: string | undefined;
$: title = mergeTooltipAndShortcut(tooltip, shortcutLabel);
2021-04-01 18:55:34 +02:00
export let onChange: (event: Event) => void;
function extendClassName(className: string): string {
return `btn ${className}`;
}
const nightMode = getContext(nightModeKey);
2021-04-14 18:33:46 +02:00
let buttonRef: HTMLButtonElement;
let inputRef: HTMLInputElement;
2021-04-14 18:33:46 +02:00
function delegateToInput() {
inputRef.click();
2021-04-14 18:33:46 +02:00
}
const dispatch = createEventDispatcher();
onMount(() => dispatch("mount", { button: buttonRef }));
</script>
<style lang="scss">
@use "ts/sass/button_mixins" as button;
2021-04-15 15:33:47 +02:00
@import "ts/sass/bootstrap/functions";
@import "ts/sass/bootstrap/variables";
2021-03-29 21:05:30 +02:00
button {
padding: 0;
2021-04-14 18:33:46 +02:00
width: calc(var(--toolbar-size) - 0px);
height: calc(var(--toolbar-size) - 0px);
padding: 4px;
2021-04-14 18:33:46 +02:00
overflow: hidden;
}
@include button.btn-day($with-disabled: false) using ($base) {
@include button.rainbow($base);
}
@include button.btn-night($with-disabled: false) using ($base) {
@include button.rainbow($base);
}
input {
display: inline-block;
cursor: pointer;
opacity: 0;
}
</style>
<button
bind:this={buttonRef}
tabindex="-1"
{id}
class={extendClassName(className)}
class:btn-day={!nightMode}
class:btn-night={nightMode}
{title}
2021-04-14 18:33:46 +02:00
on:click={delegateToInput}
on:mousedown|preventDefault>
<input tabindex="-1" bind:this={inputRef} type="color" on:change={onChange} />
2021-03-30 00:51:44 +02:00
</button>