-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathClearButton.ts
More file actions
156 lines (139 loc) · 5.51 KB
/
ClearButton.ts
File metadata and controls
156 lines (139 loc) · 5.51 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Copyright 2025 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {
CSSResultArray,
html,
SizedMixin,
TemplateResult,
} from '@spectrum-web-components/base';
import { property } from '@spectrum-web-components/base/src/decorators.js';
import { StyledButton } from './StyledButton.js';
import buttonStyles from '@spectrum-web-components/clear-button/src/clear-button.css.js';
import '@spectrum-web-components/icons-ui/icons/sp-icon-cross75.js';
import '@spectrum-web-components/icons-ui/icons/sp-icon-cross100.js';
import '@spectrum-web-components/icons-ui/icons/sp-icon-cross200.js';
import '@spectrum-web-components/icons-ui/icons/sp-icon-cross300.js';
import crossMediumStyles from '@spectrum-web-components/icon/src/spectrum-icon-cross.css.js';
const crossIcon: Record<string, () => TemplateResult> = {
s: () => html`
<sp-icon-cross75
slot="icon"
class="icon spectrum-UIIcon-Cross75"
></sp-icon-cross75>
`,
m: () => html`
<sp-icon-cross100
slot="icon"
class="icon spectrum-UIIcon-Cross100"
></sp-icon-cross100>
`,
l: () => html`
<sp-icon-cross200
slot="icon"
class="icon spectrum-UIIcon-Cross200"
></sp-icon-cross200>
`,
xl: () => html`
<sp-icon-cross300
slot="icon"
class="icon spectrum-UIIcon-Cross300"
></sp-icon-cross300>
`,
};
/**
* @element sp-clear-button
*
* @attr {string} label - Required accessible label set as aria-label
*/
export class ClearButton extends SizedMixin(StyledButton, {
noDefaultSize: true,
}) {
public static override get styles(): CSSResultArray {
return [...super.styles, buttonStyles, crossMediumStyles];
}
/**
* An accessible label that describes the component.
* It will be applied to aria-label, but not visually rendered.
* This attribute is required for clear buttons.
*/
@property()
public override label!: string;
@property({ type: Boolean, reflect: true })
public quiet = false;
/**
* The visual variant to apply to this button.
* @deprecated Use `static-color='white'` instead.
*/
@property({ reflect: true })
public set variant(variant: 'overBackground' | undefined) {
const oldValue = this._variant;
const oldStaticColor = this.staticColor;
if (variant !== 'overBackground') {
this.removeAttribute('variant');
this._variant = undefined;
this.staticColor = undefined;
return;
}
this.setAttribute('variant', variant);
this._variant = variant;
// Set staticColor to white to reflect the updated and expected attribute
this.staticColor = 'white';
if (window.__swc.DEBUG) {
window.__swc.warn(
this,
'The overBackground variant is deprecated. Please use `static-color="white"` instead.',
'https://opensource.adobe.com/spectrum-web-components/components/clear-button/',
{ level: 'deprecation' }
);
}
this.requestUpdate('variant', oldValue);
this.requestUpdate('staticColor', oldStaticColor);
}
public get variant(): 'overBackground' | undefined {
return this._variant;
}
private _variant: 'overBackground' | undefined;
/**
* The visual variant to apply to this button.
*/
@property({ reflect: true, attribute: 'static-color' })
public staticColor: 'white' | undefined;
protected override get buttonContent(): TemplateResult[] {
return [crossIcon[this.size]()];
}
protected override render(): TemplateResult {
return html`
<div class="fill">${super.render()}</div>
`;
}
public override connectedCallback(): void {
super.connectedCallback();
// Deprecation warning for default slot when content is provided
if (window.__swc.DEBUG && this.textContent?.trim()) {
window.__swc.warn(
this,
`The default slot for text content in <${this.localName}> has been deprecated and will be removed in a future release. The clear button is icon-only and does not render slot content. Use the "label" attribute instead to provide an accessible name.`,
'https://opensource.adobe.com/spectrum-web-components/components/button/#clear-button',
{ level: 'deprecation' }
);
}
// Warning for missing label attribute
if (window.__swc.DEBUG && !this.label) {
window.__swc.warn(
this,
`The "label" attribute is required on <${this.localName}> to provide an accessible name for screen readers. Please add a label attribute, e.g., <${this.localName} label="Clear">.`,
'https://opensource.adobe.com/spectrum-web-components/components/button/#clear-button',
{ level: 'high' }
);
}
}
}