-
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: Inline Add Button Component (#4270)
- Loading branch information
Showing
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
"browsers": [ | ||
{ | ||
"name": "Chromium", | ||
"version": 117 | ||
"version": 119 | ||
} | ||
] | ||
} |
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,127 @@ | ||
import '../colors/colors.js'; | ||
import '../icons/icon.js'; | ||
import '../tooltip/tooltip.js'; | ||
import { css, html, LitElement, unsafeCSS } from 'lit'; | ||
import { FocusMixin } from '../../mixins/focus/focus-mixin.js'; | ||
import { getFocusPseudoClass } from '../../helpers/focus.js'; | ||
import { getUniqueId } from '../../helpers/uniqueId.js'; | ||
import { ifDefined } from 'lit/directives/if-defined.js'; | ||
import { LocalizeCoreElement } from '../../helpers/localize-core-element.js'; | ||
import { PropertyRequiredMixin } from '../../mixins/property-required/property-required-mixin.js'; | ||
|
||
/** | ||
* A component for quickly adding items to a specific locaiton. | ||
*/ | ||
class ButtonAdd extends PropertyRequiredMixin(FocusMixin(LocalizeCoreElement(LitElement))) { | ||
static get properties() { | ||
return { | ||
/** | ||
* When text-visible is true, the text to show in the button. When text-visible is false, the text to show in the tooltip. | ||
* @type {string} | ||
*/ | ||
text: { type: String, required: true }, | ||
/** | ||
* When true, show the button with icon and visible text. When false, only show icon. | ||
* @type {boolean} | ||
*/ | ||
textVisible: { type: Boolean, reflect: true, attribute: 'text-visible' } | ||
}; | ||
} | ||
|
||
static get styles() { | ||
return css` | ||
:host { | ||
--d2l-button-add-line-style: solid; | ||
} | ||
button { | ||
align-items: center; | ||
background-color: transparent; | ||
border: 0; | ||
box-shadow: none; | ||
cursor: pointer; | ||
display: flex; | ||
font-family: inherit; | ||
justify-content: center; | ||
outline: none; | ||
padding: 0; | ||
position: relative; | ||
user-select: none; | ||
white-space: nowrap; | ||
width: 100%; | ||
} | ||
.line { | ||
border-top: 1px var(--d2l-button-add-line-style) var(--d2l-color-mica); | ||
margin: 3px 0; /** hover/click target */ | ||
width: 100%; | ||
} | ||
button:hover .line, | ||
button:${unsafeCSS(getFocusPseudoClass())} .line { | ||
border-top-color: var(--d2l-color-celestine); | ||
} | ||
.content { | ||
align-items: center; | ||
background-color: white; | ||
display: flex; | ||
position: absolute; | ||
} | ||
:host([text-visible]) .content { | ||
color: var(--d2l-color-celestine); | ||
height: 1.5rem; | ||
padding: 0 0.3rem; | ||
} | ||
:host([text-visible]) d2l-icon, | ||
:host(:not([text-visible])) button:hover d2l-icon, | ||
:host(:not([text-visible])) button:${unsafeCSS(getFocusPseudoClass())} d2l-icon { | ||
color: var(--d2l-color-celestine); | ||
} | ||
:host(:not([text-visible])) d2l-icon { | ||
color: var(--d2l-color-galena); | ||
margin: -3px; /** hover/click target */ | ||
padding: 3px; /** hover/click target */ | ||
} | ||
:host([text-visible]) d2l-icon { | ||
padding-inline-end: 0.2rem; | ||
} | ||
span { | ||
font-size: 0.7rem; | ||
font-weight: 700; | ||
letter-spacing: 0.2px; | ||
line-height: 1rem; | ||
} | ||
`; | ||
} | ||
|
||
constructor() { | ||
super(); | ||
|
||
this.textVisible = false; | ||
this._buttonId = getUniqueId(); | ||
} | ||
|
||
static get focusElementSelector() { | ||
return 'button'; | ||
} | ||
|
||
render() { | ||
const text = this.text || this.localize('components.button-add.addItem'); | ||
const id = !this.textVisible ? this._buttonId : undefined; | ||
|
||
return html` | ||
<button class="d2l-label-text" id="${ifDefined(id)}"> | ||
<div class="line"></div> | ||
<div class="content"> | ||
<d2l-icon icon="tier1:plus-default"></d2l-icon> | ||
${this.textVisible | ||
? html`<span>${text}</span>` | ||
: html`<d2l-tooltip class="vdiff-target" offset="18" for="${this._buttonId}" for-type="label">${text}</d2l-tooltip>`} | ||
</div> | ||
</button> | ||
`; | ||
} | ||
} | ||
customElements.define('d2l-button-add', ButtonAdd); | ||
|
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,73 @@ | ||
<!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-add.js'; | ||
</script> | ||
<style> | ||
.d2l-button-add-dashed { | ||
--d2l-button-add-line-style: dashed; | ||
} | ||
</style> | ||
</head> | ||
<body unresolved> | ||
|
||
<d2l-demo-page page-title="d2l-button-add"> | ||
|
||
<h2>Add Button</h2> | ||
|
||
<d2l-demo-snippet> | ||
<template> | ||
<d2l-button-add></d2l-button-add> | ||
</template> | ||
</d2l-demo-snippet> | ||
|
||
<h2>Add Button, dashed line</h2> | ||
|
||
<d2l-demo-snippet> | ||
<template> | ||
<d2l-button-add class="d2l-button-add-dashed"></d2l-button-add> | ||
</template> | ||
</d2l-demo-snippet> | ||
|
||
<h2>Add Button, custom text</h2> | ||
|
||
<d2l-demo-snippet> | ||
<template> | ||
<d2l-button-add text="Custom Tooltip"></d2l-button-add> | ||
</template> | ||
</d2l-demo-snippet> | ||
|
||
|
||
<h2>Add Button, text-visible</h2> | ||
|
||
<d2l-demo-snippet> | ||
<template> | ||
<d2l-button-add text-visible></d2l-button-add> | ||
</template> | ||
</d2l-demo-snippet> | ||
|
||
|
||
<h2>Add Button, text-visible, custom text</h2> | ||
|
||
<d2l-demo-snippet> | ||
<template> | ||
<d2l-button-add text-visible text="Custom Text"></d2l-button-add> | ||
</template> | ||
</d2l-demo-snippet> | ||
|
||
</d2l-demo-page> | ||
|
||
<script> | ||
document.addEventListener('click', e => { | ||
if (e.target.tagName !== 'D2L-BUTTON-ADD') return; | ||
console.log('add button clicked', e.target); | ||
}); | ||
</script> | ||
|
||
</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,33 @@ | ||
import '../button-add.js'; | ||
import { expect, fixture, focusElem, html } from '@brightspace-ui/testing'; | ||
|
||
describe('d2l-button-add', () => { | ||
|
||
it('default', async() => { | ||
const el = await fixture(html`<d2l-button-add></d2l-button-add>`); | ||
await expect(el).to.be.accessible(); | ||
}); | ||
|
||
it('text', async() => { | ||
const el = await fixture(html`<d2l-button-add text="Custom Text"></d2l-button-add>`); | ||
await expect(el).to.be.accessible(); | ||
}); | ||
|
||
it('visible text', async() => { | ||
const el = await fixture(html`<d2l-button-add text-visible></d2l-button-add>`); | ||
await expect(el).to.be.accessible(); | ||
}); | ||
|
||
it('focused', async() => { | ||
const el = await fixture(html`<d2l-button-add></d2l-button-add>`); | ||
await focusElem(el); | ||
await expect(el).to.be.accessible(); | ||
}); | ||
|
||
it('focused, visible text', async() => { | ||
const el = await fixture(html`<d2l-button-add text-visible></d2l-button-add>`); | ||
await focusElem(el); | ||
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,36 @@ | ||
import '../button-add.js'; | ||
import { clickElem, expect, fixture, html, oneEvent, runConstructor } from '@brightspace-ui/testing'; | ||
import { createMessage } from '../../../mixins/property-required/property-required-mixin.js'; | ||
|
||
describe('d2l-button-add', () => { | ||
|
||
describe('constructor', () => { | ||
|
||
it('should construct', () => { | ||
runConstructor('d2l-button-add'); | ||
}); | ||
|
||
}); | ||
|
||
describe('events', () => { | ||
|
||
it('dispatches click event when clicked', async() => { | ||
const el = await fixture(html`<d2l-button-add></d2l-button-add>`); | ||
setTimeout(() => clickElem(el)); | ||
await oneEvent(el, 'click'); | ||
}); | ||
|
||
it('throws error when no text', async() => { | ||
const el = await fixture(html`<d2l-button-add></d2l-button-add>`); | ||
expect(() => el.flushRequiredPropertyErrors()) | ||
.to.throw(TypeError, createMessage(el, 'text')); | ||
}); | ||
|
||
it('does not throw when text is provided', async() => { | ||
const el = await fixture(html`<d2l-button-add text="Custom Text"></d2l-button-add>`); | ||
expect(() => el.flushRequiredPropertyErrors()).to.not.throw(); | ||
}); | ||
|
||
}); | ||
|
||
}); |
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,31 @@ | ||
import '../button-add.js'; | ||
import { clickElem, expect, fixture, focusElem, hoverElem, html, oneEvent } from '@brightspace-ui/testing'; | ||
|
||
describe('button-add', () => { | ||
[ true, false ].forEach((textVisible) => { | ||
describe(`text-visible ${textVisible}`, () => { | ||
[ | ||
{ category: 'basic', template: html`<d2l-button-add ?text-visible="${textVisible}"></d2l-button-add>` }, | ||
{ category: 'text', template: html`<d2l-button-add text="Custom Text" ?text-visible="${textVisible}"></d2l-button-add>` }, | ||
{ category: 'dashed line', template: html`<d2l-button-add style="--d2l-button-add-line-style: dashed;" ?text-visible="${textVisible}"></d2l-button-add>` } | ||
].forEach(({ category, template }) => { | ||
|
||
describe(category, () => { | ||
[ | ||
{ name: 'normal' }, | ||
{ name: 'hover', action: hoverElem }, | ||
{ name: 'focus', action: focusElem }, | ||
{ name: 'click', action: clickElem } | ||
].forEach(({ action, name }) => { | ||
it(name, async() => { | ||
const elem = await fixture(template); | ||
if (action) await action(elem); | ||
if ((name === 'hover' || name === 'focus') && !elem.textVisible) await oneEvent(elem, 'd2l-tooltip-show'); | ||
await expect(elem).to.be.golden(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Binary file added
BIN
+5.23 KB
...nents/button/test/golden/button-add/chromium/text-visible-false-basic-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-add/chromium/text-visible-false-basic-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
+5.23 KB
...nents/button/test/golden/button-add/chromium/text-visible-false-basic-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
+756 Bytes
...ents/button/test/golden/button-add/chromium/text-visible-false-basic-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
+6.62 KB
...button/test/golden/button-add/chromium/text-visible-false-dashed-line-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
+6.62 KB
...button/test/golden/button-add/chromium/text-visible-false-dashed-line-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
+6.62 KB
...button/test/golden/button-add/chromium/text-visible-false-dashed-line-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
+1.77 KB
...utton/test/golden/button-add/chromium/text-visible-false-dashed-line-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
+6.75 KB
...onents/button/test/golden/button-add/chromium/text-visible-false-text-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
+6.75 KB
...onents/button/test/golden/button-add/chromium/text-visible-false-text-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
+6.75 KB
...onents/button/test/golden/button-add/chromium/text-visible-false-text-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
+756 Bytes
...nents/button/test/golden/button-add/chromium/text-visible-false-text-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.12 KB
...onents/button/test/golden/button-add/chromium/text-visible-true-basic-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.12 KB
...onents/button/test/golden/button-add/chromium/text-visible-true-basic-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.12 KB
...onents/button/test/golden/button-add/chromium/text-visible-true-basic-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.13 KB
...nents/button/test/golden/button-add/chromium/text-visible-true-basic-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.42 KB
.../button/test/golden/button-add/chromium/text-visible-true-dashed-line-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
+4.42 KB
.../button/test/golden/button-add/chromium/text-visible-true-dashed-line-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
+4.42 KB
.../button/test/golden/button-add/chromium/text-visible-true-dashed-line-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
+4.23 KB
...button/test/golden/button-add/chromium/text-visible-true-dashed-line-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.35 KB
components/button/test/golden/button-add/chromium/text-visible-true-text-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
+4.35 KB
components/button/test/golden/button-add/chromium/text-visible-true-text-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
+4.35 KB
components/button/test/golden/button-add/chromium/text-visible-true-text-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
+4.35 KB
...onents/button/test/golden/button-add/chromium/text-visible-true-text-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
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
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
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.