File size: 2,817 Bytes
0bd62e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<script lang="ts">

	import type { SelectData } from "@gradio/utils";

	import { createEventDispatcher } from "svelte";



	export let value = false;

	export let label = "Checkbox";

	export let interactive: boolean;



	const dispatch = createEventDispatcher<{

		change: boolean;

		select: SelectData;

	}>();



	// When the value changes, dispatch the change event via handle_change()

	// See the docs for an explanation: https://svelte.dev/docs/svelte-components#script-3-$-marks-a-statement-as-reactive

	$: value, dispatch("change", value);

	$: disabled = !interactive;



	async function handle_enter(

		event: KeyboardEvent & { currentTarget: EventTarget & HTMLInputElement }

	): Promise<void> {

		if (event.key === "Enter") {

			value = !value;

			dispatch("select", {

				index: 0,

				value: event.currentTarget.checked,

				selected: event.currentTarget.checked

			});

		}

	}



	async function handle_input(

		event: Event & { currentTarget: EventTarget & HTMLInputElement }

	): Promise<void> {

		value = event.currentTarget.checked;

		dispatch("select", {

			index: 0,

			value: event.currentTarget.checked,

			selected: event.currentTarget.checked

		});

	}

</script>

<label class:disabled>
	<input

		bind:checked={value}

		on:keydown={handle_enter}

		on:input={handle_input}

		{disabled}

		type="checkbox"

		name="test"

		data-testid="checkbox"

	/>
	<span class="ml-2">{label}</span>
</label>

<style>

	label {

		display: flex;

		align-items: center;

		cursor: pointer;

		color: var(--body-text-color);

		font-weight: var(--checkbox-label-text-weight);

		font-size: var(--checkbox-label-text-size);

		line-height: var(--line-md);

	}



	label > * + * {

		margin-left: var(--size-2);

	}



	input {

		--ring-color: transparent;

		position: relative;

		box-shadow: var(--input-shadow);

		border: 1px solid var(--checkbox-border-color);

		border-radius: var(--checkbox-border-radius);

		background-color: var(--checkbox-background-color);

		line-height: var(--line-sm);

	}



	input:checked,

	input:checked:hover,

	input:checked:focus {

		border-color: var(--checkbox-border-color-selected);

		background-image: var(--checkbox-check);

		background-color: var(--checkbox-background-color-selected);

	}



	input:checked:focus {

		background-image: var(--checkbox-check);

		background-color: var(--checkbox-background-color-selected);

		border-color: var(--checkbox-border-color-focus);

	}



	input:hover {

		border-color: var(--checkbox-border-color-hover);

		background-color: var(--checkbox-background-color-hover);

	}



	input:focus {

		border-color: var(--checkbox-border-color-focus);

		background-color: var(--checkbox-background-color-focus);

	}



	input[disabled],

	.disabled {

		cursor: not-allowed;

	}

</style>