Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline Add Button Component #4270

Merged
merged 20 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions components/button/button-add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import '../colors/colors.js';
import '../icons/icon.js';
import '../tooltip/tooltip.js';
import { css, html, LitElement } from 'lit';
import { FocusMixin } from '../../mixins/focus/focus-mixin.js';
import { getUniqueId } from '../../helpers/uniqueId.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { LocalizeCoreElement } from '../../helpers/localize-core-element.js';

/**
* A component for quickly adding items to a specific locaiton.
*/
class ButtonAdd extends FocusMixin(LocalizeCoreElement(LitElement)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently have this as d2l-button-add. Including inline was thought to not be great because css-wise it's not actually inline. I looked up synonyms for inline but didn't fine anything I thought was great. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I didn't use ButtonMixin. Let me know if there are other pieces of that which are important to a button that I'm missing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with not using ButtonMixin -- it just includes too many <button> attributes that would be confusing to handle here.

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 },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the PropertyRequiredMixin stuff merges, this should likely use it.

/**
* 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 {
dbatiste marked this conversation as resolved.
Show resolved Hide resolved
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 */
dbatiste marked this conversation as resolved.
Show resolved Hide resolved
width: 100%;
}
button:hover .line,
button:focus .line {
dbatiste marked this conversation as resolved.
Show resolved Hide resolved
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:focus 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)}">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just remembered randomly: make sure you set type="button" on this, otherwise it defaults to submit and will submit forms!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks! Will fix

<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, oneEvent } from '@brightspace-ui/testing';

Check failure on line 2 in components/button/test/button-add.axe.js

View workflow job for this annotation

GitHub Actions / Lint

'oneEvent' is defined but never used

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();
});

});
24 changes: 24 additions & 0 deletions components/button/test/button-add.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import '../button-add.js';
import { clickElem, fixture, html, oneEvent, runConstructor } from '@brightspace-ui/testing';

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');
});

});

});
32 changes: 32 additions & 0 deletions components/button/test/button-add.vdiff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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() => {
let elem = await fixture(template);
if (elem.tagName !== 'D2L-BUTTON-ADD') elem = elem.querySelector('d2l-button-add');
margaree marked this conversation as resolved.
Show resolved Hide resolved
if (action) await action(elem);
if ((name === 'hover' || name === 'focus') && !elem.textVisible) await oneEvent(elem, 'd2l-tooltip-show');
await expect(elem).to.be.golden();
});
});
});
});
});
});
});
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/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",
margaree marked this conversation as resolved.
Show resolved Hide resolved
"components.calendar.notSelected": "Not Selected.",
"components.calendar.selected": "Selected.",
"components.calendar.show": "Show {month}",
Expand Down
Loading