Skip to content

Commit

Permalink
feat: add button-group (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
cubmic authored Nov 7, 2023
1 parent 7d0a059 commit 6329ad5
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/components/button-group/ButtonGroup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { html, LitElement } from "lit"
import { defineElement } from "../../lib/defineElement.js"
import styles from "./button-group.css"
import { defineButtonElements } from "../button/Button.js"

/**
* @tagname leu-button-group
*/
export class LeuButtonGroup extends LitElement {
static styles = styles

static properties = {
items: { type: Array, reflect: true },
value: { type: String, reflect: true },
}

constructor() {
super()
/** @type {Array} */
this.items = []
/** @type {string} */
this.value = null
}

_setValue(newValue) {
this.value = newValue

this.dispatchEvent(
new CustomEvent("input", {
bubbles: true,
composed: true,
detail: { value: newValue },
})
)
}

render() {
return html`
<div role="menubar" class="group">
${this.items.map(
(item) =>
html`
<leu-button
label=${item}
variant=${this.value === item ? "primary" : "secondary"}
@click=${() => {
this._setValue(item)
}}
role="menuitemradio"
aria-checked=${this.value === item}
>
</leu-button>
`
)}
</div>
`
}
}

export function defineButtonGroupElements() {
defineButtonElements()
defineElement("button-group", LeuButtonGroup)
}
17 changes: 17 additions & 0 deletions src/components/button-group/button-group.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
:host,
:host * {
box-sizing: border-box;
}

:host {
--button-group-font-regular: var(--leu-font-regular);
--button-group-font-black: var(--leu-font-black);

font-family: var(--chip-font-regular);
}

.group {
display: flex;
gap: 0.5rem;
flex-wrap: wrap;
}
3 changes: 3 additions & 0 deletions src/components/button-group/leu-button-group.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineButtonGroupElements } from "./ButtonGroup.js"

defineButtonGroupElements()
41 changes: 41 additions & 0 deletions src/components/button-group/stories/button-group.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { html } from "lit"
import "../leu-button-group.js"

// https://stackoverflow.com/questions/72566428/storybook-angular-how-to-dynamically-update-args-from-the-template
import { UPDATE_STORY_ARGS } from "@storybook/core-events" // eslint-disable-line
function updateStorybookArgss(id, args) {
const channel = window.__STORYBOOK_ADDONS_CHANNEL__
channel.emit(UPDATE_STORY_ARGS, {
storyId: id,
updatedArgs: args,
})
}

export default {
title: "ButtonGroup",
component: "leu-button-group",
}

function Template({ items, value }, { id }) {
return html`
<leu-button-group
.items=${items}
.value=${value}
@click=${(event) => {
updateStorybookArgss(id, {
value: event.target.value,
})
}}
>
</leu-button-group>
<br />
<br />
<pre>value = '${value}'</pre>
`
}

export const Regular = Template.bind({})
Regular.args = {
items: ["Eins", "Zwei", "Drei"],
selected: "Zwei",
}
22 changes: 22 additions & 0 deletions src/components/button-group/test/button-group.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { html } from "lit"
import { fixture, expect } from "@open-wc/testing"

import "../leu-button-group.js"

async function defaultFixture() {
return fixture(html` <leu-button-group /> `)
}

describe("LeuButtonGroup", () => {
it("is a defined element", async () => {
const el = await customElements.get("leu-button-group")

await expect(el).not.to.be.undefined
})

it("passes the a11y audit", async () => {
const el = await defaultFixture()

await expect(el).shadowDom.to.be.accessible()
})
})

0 comments on commit 6329ad5

Please sign in to comment.