-
Notifications
You must be signed in to change notification settings - Fork 25
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
Changes from 13 commits
78583ea
5014f12
622671a
4bf83ae
c6eac4f
96efcf0
9007154
4cf9f2d
e532d55
3cc039e
d9ce259
b84a859
dce2414
370c9cb
a08cbd9
1bc32db
a8c06af
f95a9ee
2c0bf41
9fb99a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)) { | ||
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 }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once the |
||
/** | ||
* 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)}"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just remembered randomly: make sure you set There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
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> |
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'; | ||
|
||
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(); | ||
}); | ||
|
||
}); |
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'); | ||
}); | ||
|
||
}); | ||
|
||
}); |
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(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
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
. Includinginline
was thought to not be great because css-wise it's not actually inline. I looked up synonyms forinline
but didn't fine anything I thought was great. Thoughts?There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.