generated from nl-design-system/example
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
254 additions
and
1 deletion.
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,14 @@ | ||
/** | ||
* @license EUPL-1.2 | ||
* Copyright (c) 2021 Robbert Broersma | ||
*/ | ||
|
||
@import "./bem"; | ||
|
||
:host { | ||
display: block; | ||
} | ||
|
||
:host([hidden]) { | ||
display: none !important; | ||
} |
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,145 @@ | ||
<!-- | ||
@license EUPL-1.2 | ||
Copyright (c) 2021 Robbert Broersma | ||
--> | ||
|
||
import { ArgsTable, Canvas, Meta, Story } from "@storybook/addon-docs"; | ||
import "./bem.scss"; | ||
|
||
export const defaultArgs = { | ||
checked: false, | ||
disabled: false, | ||
indeterminate: false, | ||
invalid: false, | ||
required: false, | ||
value: "", | ||
}; | ||
|
||
export const CustomCheckbox = ({ | ||
checked = false, | ||
disabled = false, | ||
indeterminate = false, | ||
invalid = false, | ||
required = false, | ||
value = "", | ||
}) => | ||
`<utrecht-custom-checkbox${checked ? ' checked="true"' : ""}${disabled ? ' disabled="true"' : ""}${ | ||
indeterminate ? ' indeterminate="true"' : "" | ||
}${invalid ? ' invalid="true"' : ""}${required ? ' required="true"' : ""}${ | ||
value ? ` value="${value}"` : "" | ||
}></utrecht-custom-checkbox>`; | ||
|
||
<Meta | ||
title="Web Component/Custom Checkbox" | ||
argTypes={{ | ||
checked: { | ||
description: "Checked", | ||
control: "boolean", | ||
}, | ||
disabled: { | ||
description: "Disabled", | ||
control: "boolean", | ||
}, | ||
indeterminate: { | ||
description: "Indeterminate", | ||
control: "boolean", | ||
}, | ||
invalid: { | ||
description: "Invalid", | ||
control: "boolean", | ||
}, | ||
required: { | ||
description: "Required", | ||
control: "boolean", | ||
}, | ||
value: { | ||
description: "Set the value of the text box", | ||
control: "text", | ||
}, | ||
}} | ||
decorators={[(story) => `<form method="POST" action="#">${story()}</form>`]} | ||
parameters={{ | ||
docs: { | ||
transformSource: (_src, { args }) => CustomCheckbox(args), | ||
}, | ||
status: { | ||
type: "WORK IN PROGRESS", | ||
}, | ||
}} | ||
/> | ||
|
||
# Checkbox Component | ||
|
||
<Canvas> | ||
<Story | ||
name="Custom Checkbox" | ||
args={{ | ||
...defaultArgs, | ||
}} | ||
> | ||
{CustomCheckbox.bind({})} | ||
</Story> | ||
</Canvas> | ||
|
||
<ArgsTable story="Custom Checkbox" /> | ||
|
||
## States | ||
|
||
### Disabled | ||
|
||
<Canvas> | ||
<Story | ||
name="Disabled checkbox" | ||
args={{ | ||
...defaultArgs, | ||
disabled: true, | ||
}} | ||
> | ||
{CustomCheckbox.bind({})} | ||
</Story> | ||
</Canvas> | ||
|
||
### Checked | ||
|
||
<Canvas> | ||
<Story | ||
name="Checked checkbox" | ||
args={{ | ||
...defaultArgs, | ||
checked: true, | ||
}} | ||
> | ||
{CustomCheckbox.bind({})} | ||
</Story> | ||
</Canvas> | ||
|
||
### Indeterminate | ||
|
||
<Canvas> | ||
<Story | ||
name="Indeterminate checkbox" | ||
args={{ | ||
...defaultArgs, | ||
indeterminate: true, | ||
}} | ||
> | ||
{CustomCheckbox.bind({})} | ||
</Story> | ||
</Canvas> | ||
|
||
### Required and invalid | ||
|
||
The native checkbox should have the same position and size as the custom checkbox, so the native error message tooltip aligns with the custom checkbox. | ||
|
||
<Canvas> | ||
<Story | ||
name="Required checkbox" | ||
args={{ | ||
...defaultArgs, | ||
invalid: true, | ||
required: true, | ||
}} | ||
> | ||
{CustomCheckbox.bind({})} | ||
</Story> | ||
</Canvas> |
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,87 @@ | ||
/** | ||
* @license EUPL-1.2 | ||
* Copyright (c) 2021 Robbert Broersma | ||
*/ | ||
|
||
import { Component, Event, EventEmitter, h, Prop } from "@stencil/core"; | ||
import clsx from "clsx"; | ||
|
||
@Component({ | ||
tag: "utrecht-custom-checkbox", | ||
styleUrl: "stencil.scss", | ||
shadow: true, | ||
}) | ||
export class CustomCheckbox { | ||
@Prop({ reflect: true }) disabled: boolean = false; | ||
@Prop({ reflect: true }) checked: boolean = false; | ||
@Prop({ reflect: true }) indeterminate: boolean = false; | ||
@Prop({ reflect: true }) invalid: boolean = false; | ||
@Event() utrechtBlur: EventEmitter; | ||
@Event() utrechtChange: EventEmitter; | ||
@Event() utrechtFocus: EventEmitter; | ||
@Event() utrechtInput: EventEmitter; | ||
|
||
render() { | ||
const { checked, disabled, indeterminate, invalid } = this; | ||
const active = false; | ||
|
||
const toggleInteraction = () => { | ||
if (!this.disabled) { | ||
this.checked = !this.checked; | ||
this.utrechtInput.emit(this); | ||
this.utrechtChange.emit(this); | ||
} | ||
}; | ||
|
||
const handleClick = () => { | ||
toggleInteraction(); | ||
}; | ||
|
||
const handleKeyPress = (evt) => { | ||
if (evt.code === "Space" || evt.key === " ") { | ||
// Do not scroll the page using Space when the toggle is focussed | ||
evt.preventDefault(); | ||
} | ||
}; | ||
|
||
const handleKeyUp = (evt) => { | ||
if (evt.code === "Space" || evt.key === " ") { | ||
toggleInteraction(); | ||
} | ||
}; | ||
|
||
return ( | ||
<div | ||
class="utrecht-custom-checkbox" | ||
role="checkbox" | ||
tabIndex={disabled ? null : 0} | ||
aria-checked={indeterminate ? "mixed" : checked} | ||
aria-disabled={disabled} | ||
aria-invalid={invalid} | ||
onClick={handleClick} | ||
onKeyPress={handleKeyPress} | ||
onKeyUp={handleKeyUp} | ||
> | ||
<div | ||
class={clsx( | ||
"utrecht-custom-checkbox__box", | ||
active && "utrecht-custom-checkbox__box--active", | ||
checked && "utrecht-custom-checkbox__box--checked", | ||
!checked && "utrecht-custom-checkbox__box--not-checked", | ||
disabled && "utrecht-custom-checkbox__box--disabled", | ||
focus && "utrecht-custom-checkbox__box--focus", | ||
invalid && "utrecht-custom-checkbox__box--invalid", | ||
indeterminate && "utrecht-custom-checkbox__box--indeterminate" | ||
)} | ||
> | ||
<utrecht-icon-checkmark class="utrecht-custom-checkbox__icon utrecht-custom-checkbox__icon--checked"> | ||
✔ | ||
</utrecht-icon-checkmark> | ||
<utrecht-icon-indeterminate class="utrecht-custom-checkbox__icon utrecht-custom-checkbox__icon--indeterminate"> | ||
■ | ||
</utrecht-icon-indeterminate> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} |
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