-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add sd-checkbox * refactor: remove label of switch in favour of textContext * feat: add support for sd-label * refactor: align checkbox and switch rendering * feat: improve margin management * fix: top/bottom margin * feat: add sd-checkboxgroup * docs: minor re-wording * refactor: update option-type elements to all utilize textContent as their label * refactor: update option-type elements to all utilize textContent as their label --------- Co-authored-by: Richard Herman <[email protected]>
- Loading branch information
Showing
8 changed files
with
391 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { css, html, LitElement, type TemplateResult } from "lit"; | ||
import { customElement } from "lit/decorators.js"; | ||
import { repeat } from "lit/directives/repeat.js"; | ||
|
||
import { Input } from "../mixins/input"; | ||
import { List } from "../mixins/list"; | ||
import { SDCheckboxElement } from "./checkbox"; | ||
import { type SDOptionElement } from "./option"; | ||
|
||
/** | ||
* Element that offers persisting an set of values, from a group of checkbox options. | ||
*/ | ||
@customElement("sd-checkboxgroup") | ||
export class SDCheckboxGroupElement extends List(Input<(boolean | number | string)[]>(LitElement)) { | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public static styles = [ | ||
super.styles ?? [], | ||
css` | ||
sd-checkbox { | ||
display: flex; | ||
} | ||
`, | ||
]; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public override render(): TemplateResult { | ||
return html` | ||
${repeat( | ||
this.items, | ||
(opt) => opt, | ||
(opt) => { | ||
return html`<sd-checkbox | ||
.checked=${(this.value ?? []).findIndex((value) => value === opt.value) > -1} | ||
.disabled=${opt.disabled} | ||
.label=${opt.label} | ||
@change=${(ev: Event): void => { | ||
if (ev.target instanceof SDCheckboxElement) { | ||
this.#handleChange(ev.target.checked, opt.value); | ||
} | ||
}} | ||
/>`; | ||
}, | ||
)} | ||
`; | ||
} | ||
|
||
/** | ||
* Handles a checkbox state changing. | ||
* @param checked Whether the checkbox is checked. | ||
* @param value Value the checkbox represents. | ||
*/ | ||
#handleChange(checked: boolean, value: SDOptionElement["value"]): void { | ||
if (value === undefined) { | ||
return; | ||
} | ||
|
||
const values = new Set(this.value); | ||
if (checked) { | ||
values.add(value); | ||
} else { | ||
values.delete(value); | ||
} | ||
|
||
this.value = Array.from(values); | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
/** | ||
* Element that offers persisting an set of values, from a group of checkbox options. | ||
*/ | ||
"sd-checkboxgroup": SDCheckboxGroupElement; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import { css, html, LitElement, type TemplateResult } from "lit"; | ||
import { customElement } from "lit/decorators.js"; | ||
import { ifDefined } from "lit/directives/if-defined.js"; | ||
import { ref } from "lit/directives/ref.js"; | ||
|
||
import { Input } from "../mixins/input"; | ||
import { Labeled } from "../mixins/labeled"; | ||
import { type HTMLInputEvent, preventDoubleClickSelection } from "../utils"; | ||
|
||
/** | ||
* Element that offers persisting a `boolean` via a checkbox. | ||
*/ | ||
@customElement("sd-checkbox") | ||
export class SDCheckboxElement extends Labeled(Input(LitElement)) { | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public static styles = [ | ||
super.styles ?? [], | ||
css` | ||
/** | ||
* Container | ||
*/ | ||
:host { | ||
display: inline-flex; | ||
} | ||
label { | ||
align-items: center; | ||
display: inline-flex; | ||
margin: var(--space-xs) 0; | ||
outline: none; | ||
&:focus-visible .checkbox { | ||
box-shadow: var(--highlight-box-shadow); | ||
outline: var(--highlight-outline--focus); | ||
outline-offset: var(--highlight-outline-offset); | ||
} | ||
&:has(input:disabled) { | ||
color: var(--color-content-disabled); | ||
} | ||
} | ||
/** | ||
* Checkbox and text | ||
*/ | ||
.checkbox { | ||
border: solid 1px var(--color-border-strong); | ||
border-radius: var(--rounding-m); | ||
box-sizing: border-box; | ||
height: var(--size-m); | ||
width: var(--size-m); | ||
user-select: none; | ||
} | ||
.checkbox > svg { | ||
visibility: hidden; | ||
} | ||
.text { | ||
margin-left: var(--space-xs); | ||
} | ||
/** | ||
* States | ||
*/ | ||
input { | ||
display: none; | ||
/* Checked */ | ||
&:checked + .checkbox { | ||
border-width: 0; | ||
background-color: var(--color-surface-accent); | ||
color: var(--color-content-ondark); | ||
& > svg { | ||
visibility: visible; | ||
} | ||
} | ||
/* Disabled */ | ||
&:disabled { | ||
& + .checkbox { | ||
border-color: var(--color-border-subtle-disabled); | ||
} | ||
&:checked + .checkbox { | ||
background-color: var(--color-surface-disabled); | ||
color: var(--color-content-disabled); | ||
} | ||
} | ||
} | ||
`, | ||
]; | ||
|
||
/** | ||
* Initializes a new instance of the {@link SDCheckboxElement} class. | ||
*/ | ||
constructor() { | ||
super(); | ||
this.role = "checkbox"; | ||
} | ||
|
||
/** | ||
* Gets the checked state. | ||
* @returns `true` when the checkbox is checked; otherwise `false`. | ||
*/ | ||
public get checked(): boolean { | ||
return !!this.value; | ||
} | ||
|
||
/** | ||
* Sets the checked state. | ||
* @param value Value indicating whether the checkbox is checked. | ||
*/ | ||
public set checked(value: boolean) { | ||
this.value = value; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public override click(): void { | ||
if (!this.disabled) { | ||
this.checked = !this.checked; | ||
} | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public override render(): TemplateResult { | ||
return html` | ||
<label | ||
tabindex=${ifDefined(this.disabled ? undefined : 0)} | ||
@mousedown=${preventDoubleClickSelection} | ||
@keydown=${(ev: KeyboardEvent): void => { | ||
// Toggle switch on space bar key. | ||
if (ev.code === "Space") { | ||
this.checked = !this.checked; | ||
ev.preventDefault(); | ||
} | ||
}} | ||
> | ||
<input | ||
${ref(this.inputRef)} | ||
type="checkbox" | ||
.checked=${this.checked} | ||
.disabled=${this.disabled} | ||
@change=${(ev: HTMLInputEvent<HTMLInputElement>): void => { | ||
this.checked = ev.target.checked; | ||
this.dispatchEvent(new Event("change")); // TODO: relocate this to Input for closed shadow roots | ||
}} | ||
/> | ||
<div class="checkbox" role="checkbox"> | ||
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" fill="currentColor" viewBox="0 0 24 24"> | ||
<path | ||
d="M19.78 7.22a.75.75 0 0 1 0 1.06l-9.5 9.5a.75.75 0 0 1-1.06 0l-5-5a.75.75 0 1 1 1.06-1.06l4.47 4.47 8.97-8.97a.75.75 0 0 1 1.06 0Z" | ||
/> | ||
</svg> | ||
</div> | ||
${this.label && html`<span class="text">${this.label}</span>`} | ||
</label> | ||
`; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
/** | ||
* Element that offers persisting a `boolean` via a checkbox. | ||
*/ | ||
"sd-checkbox": SDCheckboxElement; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.