-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add new d2l-button-toggle component. (#5048)
- Loading branch information
Showing
33 changed files
with
321 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { css, html, LitElement } from 'lit'; | ||
|
||
/** | ||
* A button container component for button toggles. | ||
*/ | ||
class ButtonToggle extends LitElement { | ||
|
||
static get properties() { | ||
return { | ||
/** | ||
* Pressed state | ||
* @type {boolean} | ||
*/ | ||
pressed: { type: Boolean, reflect: true } | ||
}; | ||
} | ||
|
||
static get styles() { | ||
return css` | ||
:host { | ||
display: inline-block; | ||
} | ||
:host([hidden]) { | ||
display: none; | ||
} | ||
::slotted(:not(d2l-button-icon, d2l-button-subtle)), | ||
:host slot[name="pressed"], | ||
:host([pressed]) slot[name="not-pressed"] { | ||
display: none; | ||
} | ||
:host slot[name="not-pressed"], | ||
:host([pressed]) slot[name="pressed"] { | ||
display: contents; | ||
} | ||
`; | ||
} | ||
|
||
constructor() { | ||
super(); | ||
this.pressed = false; | ||
} | ||
|
||
firstUpdated(changedProperties) { | ||
super.firstUpdated(changedProperties); | ||
if (this._focusOnFirstRender) { | ||
this._focusOnFirstRender = false; | ||
this.focus(); | ||
} | ||
} | ||
|
||
render() { | ||
return html` | ||
<slot @click="${this._handleNotPressedClick}" name="not-pressed"></slot> | ||
<slot @click="${this._handlePressedClick}" name="pressed"></slot> | ||
`; | ||
} | ||
|
||
updated(changedProperties) { | ||
super.updated(changedProperties); | ||
|
||
if (changedProperties.get('pressed') === undefined) return; | ||
|
||
/** Dispatched when the pressed state changes */ | ||
this.dispatchEvent(new CustomEvent('d2l-button-toggle-change')); | ||
} | ||
|
||
focus() { | ||
if (!this.hasUpdated) { | ||
this._focusOnFirstRender = true; | ||
return; | ||
} | ||
|
||
const elem = this.shadowRoot.querySelector(this.pressed ? 'slot[name="pressed"]' : 'slot[name="not-pressed"]').assignedNodes()[0]; | ||
if (!elem) { | ||
throw new Error('d2l-button-toggle: no button to focus'); | ||
} | ||
|
||
elem.focus(); | ||
} | ||
|
||
async _handleClick(pressed) { | ||
this.pressed = pressed; | ||
await this.updateComplete; | ||
this.focus(); | ||
} | ||
|
||
_handleNotPressedClick() { | ||
this._handleClick(true); | ||
} | ||
|
||
_handlePressedClick() { | ||
this._handleClick(false); | ||
} | ||
|
||
} | ||
|
||
customElements.define('d2l-button-toggle', ButtonToggle); |
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,60 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta charset="UTF-8"> | ||
<link rel="stylesheet" href="../../demo/styles.css" type="text/css"> | ||
<script type="module"> | ||
import '../../demo/demo-page.js'; | ||
import '../button-icon.js'; | ||
import '../button-subtle.js'; | ||
import '../button-toggle.js'; | ||
</script> | ||
</head> | ||
<body unresolved> | ||
|
||
<d2l-demo-page page-title="d2l-button-toggle"> | ||
|
||
<h2>Toggle Button (using d2l-button-icon)</h2> | ||
|
||
<d2l-demo-snippet> | ||
<template> | ||
<d2l-button-toggle id="toggle-button-icon"> | ||
<d2l-button-icon slot="not-pressed" icon="tier1:pin-hollow" text="Unpinned, click to pin."></d2l-button-icon> | ||
<d2l-button-icon slot="pressed" icon="tier1:pin-filled" text="Pinned, click to unpin."></d2l-button-icon> | ||
</d2l-button-toggle> | ||
<script> | ||
document.querySelector('#toggle-button-icon').addEventListener('d2l-button-toggle-change', e => console.log(e)); | ||
</script> | ||
</template> | ||
</d2l-demo-snippet> | ||
|
||
<h2>Toggle Button (using d2l-button-subtle)</h2> | ||
|
||
<d2l-demo-snippet> | ||
<template> | ||
<d2l-button-toggle id="toggle-button-subtle" pressed> | ||
<d2l-button-subtle slot="not-pressed" icon="tier1:lock-unlock" text="Unlocked" description="Click to lock."></d2l-button-subtle> | ||
<d2l-button-subtle slot="pressed" icon="tier1:lock-locked" text="Locked" description="Click to unlock."></d2l-button-subtle> | ||
</d2l-button-toggle> | ||
<script> | ||
document.querySelector('#toggle-button-subtle').addEventListener('d2l-button-toggle-change', e => console.log(e)); | ||
</script> | ||
</template> | ||
</d2l-demo-snippet> | ||
|
||
<h2>Toggle Button (disabled)</h2> | ||
|
||
<d2l-demo-snippet> | ||
<template> | ||
<d2l-button-toggle pressed> | ||
<d2l-button-subtle slot="not-pressed" disabled icon="tier1:subscribe-hollow" text="Not Subscribed" description="Click to subscribe."></d2l-button-subtle> | ||
<d2l-button-subtle slot="pressed" disabled icon="tier1:subscribe-filled" text="Subscribed" description="Click to unsubscribe."></d2l-button-subtle> | ||
</d2l-button-toggle> | ||
</template> | ||
</d2l-demo-snippet> | ||
|
||
</d2l-demo-page> | ||
|
||
</body> | ||
</html> |
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,26 @@ | ||
import '../button-icon.js'; | ||
import '../button-toggle.js'; | ||
import { expect, fixture, html } from '@brightspace-ui/testing'; | ||
|
||
describe('d2l-button-toggle', () => { | ||
|
||
const normalFixture = html` | ||
<d2l-button-toggle> | ||
<d2l-button-icon slot="not-pressed" icon="tier1:pin-hollow" text="Unpinned, click to pin."></d2l-button-icon> | ||
<d2l-button-icon slot="pressed" icon="tier1:pin-filled" text="Pinned, click to unpin."></d2l-button-icon> | ||
</d2l-button-toggle> | ||
`; | ||
|
||
it('not pressed', async() => { | ||
const el = await fixture(normalFixture); | ||
await expect(el).to.be.accessible(); | ||
}); | ||
|
||
it('pressed', async() => { | ||
const el = await fixture(normalFixture); | ||
el.pressed = true; | ||
await el.updateComplete; | ||
await expect(el).to.be.accessible(); | ||
}); | ||
|
||
}); |
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,66 @@ | ||
import '../button-icon.js'; | ||
import '../button-toggle.js'; | ||
import { clickElem, expect, fixture, html, oneEvent, runConstructor } from '@brightspace-ui/testing'; | ||
|
||
describe('d2l-button-toggle', () => { | ||
|
||
describe('constructor', () => { | ||
|
||
it('should construct', () => { | ||
runConstructor('d2l-button-toggle'); | ||
}); | ||
|
||
}); | ||
|
||
describe('events', () => { | ||
|
||
it('dispatches d2l-button-toggle-change event not-pressed is clicked', async() => { | ||
const el = await fixture(html` | ||
<d2l-button-toggle> | ||
<d2l-button-icon slot="not-pressed" icon="tier1:pin-hollow" text="Unpinned, click to pin."></d2l-button-icon> | ||
<d2l-button-icon slot="pressed" icon="tier1:pin-filled" text="Pinned, click to unpin."></d2l-button-icon> | ||
</d2l-button-toggle> | ||
`); | ||
clickElem(el.querySelector('[slot="not-pressed"]')); | ||
const e = await oneEvent(el, 'd2l-button-toggle-change'); | ||
expect(e.target.pressed).to.equal(true); | ||
}); | ||
|
||
it('dispatches d2l-button-toggle-change event pressed is clicked', async() => { | ||
const el = await fixture(html` | ||
<d2l-button-toggle pressed> | ||
<d2l-button-icon slot="not-pressed" icon="tier1:pin-hollow" text="Unpinned, click to pin."></d2l-button-icon> | ||
<d2l-button-icon slot="pressed" icon="tier1:pin-filled" text="Pinned, click to unpin."></d2l-button-icon> | ||
</d2l-button-toggle> | ||
`); | ||
clickElem(el.querySelector('[slot="pressed"]')); | ||
const e = await oneEvent(el, 'd2l-button-toggle-change'); | ||
expect(e.target.pressed).to.equal(false); | ||
}); | ||
|
||
it('does not dispatch d2l-button-toggle-change event initially', async() => { | ||
let dispatched = false; | ||
const el = document.createElement('d2l-button-toggle'); | ||
el.addEventListener('d2l-button-toggle-change', () => dispatched = true); | ||
document.body.appendChild(el); | ||
await el.updateComplete; | ||
expect(dispatched).to.equal(false); | ||
}); | ||
|
||
it('does not dispatch d2l-button-toggle-change event if disabled buttons are clicked', async() => { | ||
const el = await fixture(html` | ||
<d2l-button-toggle> | ||
<d2l-button-icon slot="not-pressed" disabled icon="tier1:pin-hollow" text="Unpinned, click to pin."></d2l-button-icon> | ||
<d2l-button-icon slot="pressed" disabled icon="tier1:pin-filled" text="Pinned, click to unpin."></d2l-button-icon> | ||
</d2l-button-toggle> | ||
`); | ||
let dispatched = false; | ||
el.addEventListener('d2l-button-toggle-change', () => dispatched = true); | ||
await clickElem(el.querySelector('[slot="not-pressed"]')); | ||
expect(el.pressed).to.equal(false); | ||
expect(dispatched).to.be.false; | ||
}); | ||
|
||
}); | ||
|
||
}); |
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,39 @@ | ||
import '../button-icon.js'; | ||
import '../button-subtle.js'; | ||
import '../button-toggle.js'; | ||
import { clickElem, expect, fixture, focusElem, hoverElem, html, sendKeysElem } from '@brightspace-ui/testing'; | ||
|
||
describe('button-toggle', () => { | ||
|
||
[ | ||
{ category: 'button-icon', template: html`<d2l-button-toggle><d2l-button-icon slot="not-pressed" icon="tier1:pin-hollow" text="Unpinned, click to pin."></d2l-button-icon><d2l-button-icon slot="pressed" icon="tier1:pin-filled" text="Pinned, click to unpin."></d2l-button-icon></d2l-button-toggle>` }, | ||
{ category: 'button-icon-pressed', template: html`<d2l-button-toggle pressed><d2l-button-icon slot="not-pressed" icon="tier1:pin-hollow" text="Unpinned, click to pin."></d2l-button-icon><d2l-button-icon slot="pressed" icon="tier1:pin-filled" text="Pinned, click to unpin."></d2l-button-icon></d2l-button-toggle>` }, | ||
{ category: 'button-subtle', template: html`<d2l-button-toggle><d2l-button-subtle slot="not-pressed" icon="tier1:lock-unlock" text="Unlocked" description="Click to lock."></d2l-button-subtle><d2l-button-subtle slot="pressed" icon="tier1:lock-locked" text="Locked" description="Click to unlock."></d2l-button-subtle></d2l-button-toggle>` }, | ||
{ category: 'button-subtle-pressed', template: html`<d2l-button-toggle pressed><d2l-button-subtle slot="not-pressed" icon="tier1:lock-unlock" text="Unlocked" description="Click to lock."></d2l-button-subtle><d2l-button-subtle slot="pressed" icon="tier1:lock-locked" text="Locked" description="Click to unlock."></d2l-button-subtle></d2l-button-toggle>` }, | ||
{ category: 'button-subtle-disabled', template: html`<d2l-button-toggle><d2l-button-subtle slot="not-pressed" disabled icon="tier1:lock-unlock" text="Unlocked" description="Click to lock."></d2l-button-subtle><d2l-button-subtle slot="pressed" disabled icon="tier1:lock-locked" text="Locked" description="Click to unlock."></d2l-button-subtle></d2l-button-toggle>` } | ||
].forEach(({ category, template }) => { | ||
|
||
const getActiveButton = elem => { | ||
if (elem.pressed) return elem.querySelector('[slot="pressed"]'); | ||
else return elem.querySelector('[slot="not-pressed"]'); | ||
}; | ||
|
||
describe(category, () => { | ||
[ | ||
{ name: 'normal' }, | ||
{ name: 'hover', action: hoverElem }, | ||
{ name: 'focus', action: focusElem }, | ||
{ name: 'click', action: elem => clickElem(getActiveButton(elem)) }, | ||
{ name: 'enter', action: elem => sendKeysElem(getActiveButton(elem), 'press', 'Enter') } | ||
].forEach(({ action, name }) => { | ||
it(name, async() => { | ||
const elem = await fixture(template); | ||
if (action) await action(elem); | ||
await expect(elem).to.be.golden(); | ||
}); | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
}); |
Binary file added
BIN
+1.05 KB
components/button/test/golden/button-toggle/chromium/button-icon-click.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.37 KB
components/button/test/golden/button-toggle/chromium/button-icon-enter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.41 KB
components/button/test/golden/button-toggle/chromium/button-icon-focus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.09 KB
components/button/test/golden/button-toggle/chromium/button-icon-hover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+749 Bytes
components/button/test/golden/button-toggle/chromium/button-icon-normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.09 KB
components/button/test/golden/button-toggle/chromium/button-icon-pressed-click.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.41 KB
components/button/test/golden/button-toggle/chromium/button-icon-pressed-enter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.37 KB
components/button/test/golden/button-toggle/chromium/button-icon-pressed-focus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.05 KB
components/button/test/golden/button-toggle/chromium/button-icon-pressed-hover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+705 Bytes
...onents/button/test/golden/button-toggle/chromium/button-icon-pressed-normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+3.38 KB
components/button/test/golden/button-toggle/chromium/button-subtle-click.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+3.26 KB
...ents/button/test/golden/button-toggle/chromium/button-subtle-disabled-click.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+3.26 KB
...ents/button/test/golden/button-toggle/chromium/button-subtle-disabled-enter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+3.26 KB
...ents/button/test/golden/button-toggle/chromium/button-subtle-disabled-focus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+3.26 KB
...ents/button/test/golden/button-toggle/chromium/button-subtle-disabled-hover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+3.26 KB
...nts/button/test/golden/button-toggle/chromium/button-subtle-disabled-normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+4.67 KB
components/button/test/golden/button-toggle/chromium/button-subtle-enter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+5.23 KB
components/button/test/golden/button-toggle/chromium/button-subtle-focus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+3.95 KB
components/button/test/golden/button-toggle/chromium/button-subtle-hover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+3.52 KB
components/button/test/golden/button-toggle/chromium/button-subtle-normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+3.95 KB
...nents/button/test/golden/button-toggle/chromium/button-subtle-pressed-click.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+5.23 KB
...nents/button/test/golden/button-toggle/chromium/button-subtle-pressed-enter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+4.67 KB
...nents/button/test/golden/button-toggle/chromium/button-subtle-pressed-focus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+3.38 KB
...nents/button/test/golden/button-toggle/chromium/button-subtle-pressed-hover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.98 KB
...ents/button/test/golden/button-toggle/chromium/button-subtle-pressed-normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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