This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 762
/
close-button.element.ts
84 lines (72 loc) · 2.12 KB
/
close-button.element.ts
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
79
80
81
82
83
84
/*
* Copyright (c) 2016-2021 VMware, Inc. All Rights Reserved.
* This software is released under MIT license.
* The full license information can be found in LICENSE in the root directory of this project.
*/
import { html } from 'lit';
import { CdsBaseButton, baseStyles, isBrowser, property, setAttributes, HTMLAttributeTuple } from '@cds/core/internal';
import { ClarityIcons } from '@cds/core/icon/icon.service.js';
import { timesIcon } from '@cds/core/icon/shapes/times.js';
import styles from './close-button.element.scss';
export const CdsCloseButtonTagName = 'cds-internal-close-button';
export function appendCloseButton(
hostElement: HTMLElement,
attributes?: HTMLAttributeTuple[],
clickHandler?: () => void
) {
if (isBrowser() && !!hostElement) {
const closeBtn = document.createElement(CdsCloseButtonTagName);
if (attributes && attributes.length > 0) {
setAttributes(closeBtn, ...attributes);
}
if (clickHandler) {
closeBtn.addEventListener('click', clickHandler.bind(hostElement));
}
hostElement.appendChild(closeBtn);
}
}
export function removeCloseButton(hostElement: HTMLElement) {
if (isBrowser() && !!hostElement) {
const closeBtn = hostElement.querySelector(CdsCloseButtonTagName);
if (closeBtn) {
hostElement.removeChild(closeBtn);
}
}
}
/**
* Standard close button for Clarity Components
*
* ```typescript
* import '@cds/core/internal-components/close-button/register.js';
* ```
*
* ```html
* <cds-internal-close-button></cds-internal-close-button>
* ```
*
* @element cds-internal-close-button
* @cssprop --background
* @cssprop --color
* @cssprop --opacity
* @cssprop --padding
*/
export class CdsInternalCloseButton extends CdsBaseButton {
@property({ type: String })
iconSize = '18';
@property({ type: String })
iconShape = 'times';
render() {
return html`
<div class="private-host">
<cds-icon shape="${this.iconShape}" size="${this.iconSize}"></cds-icon>
</div>
`;
}
static get styles() {
return [baseStyles, styles];
}
constructor() {
super();
ClarityIcons.addIcons(timesIcon);
}
}