-
Notifications
You must be signed in to change notification settings - Fork 25
/
list-item-link-mixin.js
78 lines (67 loc) · 2.13 KB
/
list-item-link-mixin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import '../colors/colors.js';
import { css, html } from 'lit';
import { getUniqueId } from '../../helpers/uniqueId.js';
import { ListItemMixin } from './list-item-mixin.js';
export const ListItemLinkMixin = superclass => class extends ListItemMixin(superclass) {
static get properties() {
return {
/**
* Address of item link if navigable
* @type {string}
*/
actionHref: { type: String, attribute: 'action-href', reflect: true }
};
}
static get styles() {
const styles = [ css`
:host([action-href]:not([action-href=""])) {
--d2l-list-item-content-text-color: var(--d2l-color-celestine);
}
a[href] {
display: block;
height: 100%;
outline: none;
width: 100%;
}
:host([action-href]:not([action-href=""])) [slot="content"],
:host(:not([no-primary-action])) [slot="control-action"] ~ [slot="content"],
:host(:not([no-primary-action])) [slot="outside-control-action"] ~ [slot="content"] {
pointer-events: none;
}
:host([action-href]:not([action-href=""])) [slot="control-action"],
:host([action-href]:not([action-href=""])) [slot="outside-control-action"] {
grid-column-end: control-end;
}
` ];
super.styles && styles.unshift(super.styles);
return styles;
}
constructor() {
super();
this.actionHref = null;
this._primaryActionId = getUniqueId();
}
willUpdate(changedProperties) {
super.willUpdate(changedProperties);
if (changedProperties.has('actionHref') && !this.actionHref) this._hoveringPrimaryAction = false;
}
_handleLinkClick() {
/** Dispatched when the item's primary link action is clicked */
this.dispatchEvent(new CustomEvent('d2l-list-item-link-click', { bubbles: true }));
}
_handleLinkKeyDown(e) {
if (e.keyCode !== 32) return;
// handle the space key
e.preventDefault();
e.stopPropagation();
this.shadowRoot.querySelector(`#${this._primaryActionId}`).click();
}
_renderPrimaryAction(labelledBy) {
if (!this.actionHref) return;
return html`<a aria-labelledby="${labelledBy}"
@click="${this._handleLinkClick}"
href="${this.actionHref}"
id="${this._primaryActionId}"
@keydown="${this._handleLinkKeyDown}"></a>`;
}
};