Skip to content

Commit

Permalink
feat: Inline Add Button Component (#4270)
Browse files Browse the repository at this point in the history
  • Loading branch information
margaree authored Nov 24, 2023
1 parent a9d3343 commit 6124689
Show file tree
Hide file tree
Showing 50 changed files with 321 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .vdiff.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"browsers": [
{
"name": "Chromium",
"version": 117
"version": 119
}
]
}
127 changes: 127 additions & 0 deletions components/button/button-add.js
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);

73 changes: 73 additions & 0 deletions components/button/demo/button-add.html
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>
33 changes: 33 additions & 0 deletions components/button/test/button-add.axe.js
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();
});

});
36 changes: 36 additions & 0 deletions components/button/test/button-add.test.js
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();
});

});

});
31 changes: 31 additions & 0 deletions components/button/test/button-add.vdiff.js
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();
});
});
});
});
});
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ <h2 class="d2l-heading-3">Components</h2>
Buttons
<ul>
<li><a href="components/button/demo/button.html">d2l-button</a></li>
<li><a href="components/button/demo/button-add.html">d2l-button-add</a></li>
<li><a href="components/button/demo/button-icon.html">d2l-button-icon</a></li>
<li><a href="components/button/demo/button-subtle.html">d2l-button-subtle</a></li>
<li><a href="components/button/demo/floating-buttons.html">d2l-floating-buttons</a></li>
Expand Down
1 change: 1 addition & 0 deletions lang/ar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default {
"components.alert.close": "إغلاق التنبيه",
"components.breadcrumbs.breadcrumb": "شريط التنقل",
"components.button-add.addItem": "Add Item",
"components.calendar.notSelected": "لم يتم التحديد.",
"components.calendar.selected": "تم التحديد.",
"components.calendar.show": "إظهار {month}",
Expand Down
1 change: 1 addition & 0 deletions lang/cy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default {
"components.alert.close": "Cau Hysbysiad",
"components.breadcrumbs.breadcrumb": "Briwsionyn Bara",
"components.button-add.addItem": "Add Item",
"components.calendar.notSelected": "Heb ei Ddewis.",
"components.calendar.selected": "Wedi'i Ddewis.",
"components.calendar.show": "Dangos {month}",
Expand Down
1 change: 1 addition & 0 deletions lang/da.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default {
"components.alert.close": "Luk besked",
"components.breadcrumbs.breadcrumb": "Brødkrumme",
"components.button-add.addItem": "Add Item",
"components.calendar.notSelected": "Ikke valgt.",
"components.calendar.selected": "Valgt.",
"components.calendar.show": "Vis {month}",
Expand Down
1 change: 1 addition & 0 deletions lang/de.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default {
"components.alert.close": "Benachrichtigung schließen",
"components.breadcrumbs.breadcrumb": "Brotkrümelnavigation",
"components.button-add.addItem": "Add Item",
"components.calendar.notSelected": "Nicht ausgewählt.",
"components.calendar.selected": "Ausgewählt.",
"components.calendar.show": "{month} anzeigen",
Expand Down
1 change: 1 addition & 0 deletions lang/en-gb.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default {
"components.alert.close": "Close Alert",
"components.breadcrumbs.breadcrumb": "Breadcrumb",
"components.button-add.addItem": "Add Item",
"components.calendar.notSelected": "Not Selected.",
"components.calendar.selected": "Selected.",
"components.calendar.show": "Show {month}",
Expand Down
1 change: 1 addition & 0 deletions lang/en.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default {
"components.alert.close": "Close Alert",
"components.breadcrumbs.breadcrumb": "Breadcrumb",
"components.button-add.addItem": "Add Item",
"components.calendar.notSelected": "Not Selected.",
"components.calendar.selected": "Selected.",
"components.calendar.show": "Show {month}",
Expand Down
1 change: 1 addition & 0 deletions lang/es-es.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default {
"components.alert.close": "Cerrar alerta",
"components.breadcrumbs.breadcrumb": "Ruta de navegación",
"components.button-add.addItem": "Add Item",
"components.calendar.notSelected": "No seleccionado.",
"components.calendar.selected": "Seleccionado.",
"components.calendar.show": "Mostrar {month}",
Expand Down
1 change: 1 addition & 0 deletions lang/es.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default {
"components.alert.close": "Cerrar alerta",
"components.breadcrumbs.breadcrumb": "Ruta de navegación",
"components.button-add.addItem": "Add Item",
"components.calendar.notSelected": "No seleccionado.",
"components.calendar.selected": "Seleccionado.",
"components.calendar.show": "Mostrar {month}",
Expand Down
1 change: 1 addition & 0 deletions lang/fr-fr.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default {
"components.alert.close": "Fermer l'alerte",
"components.breadcrumbs.breadcrumb": "Chemin de navigation",
"components.button-add.addItem": "Add Item",
"components.calendar.notSelected": "Non sélectionné.",
"components.calendar.selected": "Sélectionné.",
"components.calendar.show": "Afficher {month}",
Expand Down
Loading

0 comments on commit 6124689

Please sign in to comment.