Skip to content

Commit 275dca6

Browse files
committed
fix(clear-button): add deprecation warning
1 parent 68b03fb commit 275dca6

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

1st-gen/packages/button/src/ClearButton.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ export class ClearButton extends SizedMixin(StyledButton, {
6464
return [...super.styles, buttonStyles, crossMediumStyles];
6565
}
6666

67+
/**
68+
* An accessible label that describes the component.
69+
* It will be applied to aria-label, but not visually rendered.
70+
* This attribute is required for clear buttons.
71+
*/
72+
@property()
73+
public override label!: string;
74+
6775
@property({ type: Boolean, reflect: true })
6876
public quiet = false;
6977

@@ -121,4 +129,28 @@ export class ClearButton extends SizedMixin(StyledButton, {
121129
<div class="fill">${super.render()}</div>
122130
`;
123131
}
132+
133+
public override connectedCallback(): void {
134+
super.connectedCallback();
135+
136+
// Deprecation warning for default slot when content is provided
137+
if (window.__swc.DEBUG && this.textContent?.trim()) {
138+
window.__swc.warn(
139+
this,
140+
`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.`,
141+
'https://opensource.adobe.com/spectrum-web-components/components/button/#clear-button',
142+
{ level: 'deprecation' }
143+
);
144+
}
145+
146+
// Warning for missing label attribute
147+
if (window.__swc.DEBUG && !this.label) {
148+
window.__swc.warn(
149+
this,
150+
`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">.`,
151+
'https://opensource.adobe.com/spectrum-web-components/components/button/#clear-button',
152+
{ level: 'high' }
153+
);
154+
}
155+
}
124156
}

1st-gen/packages/button/test/clear-button.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,45 @@ describe('Clear Button', () => {
112112
expect(consoleStub).to.be.calledOnce;
113113
expect(warning.includes(expectedContent)).to.be.true;
114114
});
115+
116+
it('should log deprecation warning when slot content is provided', async () => {
117+
const el = await fixture<ClearButton>(html`
118+
<sp-clear-button label="Clear">Clear</sp-clear-button>
119+
`);
120+
121+
await elementUpdated(el);
122+
123+
const warning = consoleStub.getCall(0).args.at(0);
124+
const expectedContent =
125+
'The default slot for text content in <sp-clear-button> has been deprecated';
126+
127+
expect(consoleStub).to.be.calledOnce;
128+
expect(warning.includes(expectedContent)).to.be.true;
129+
});
130+
131+
it('should log warning when label attribute is missing', async () => {
132+
const el = await fixture<ClearButton>(html`
133+
<sp-clear-button></sp-clear-button>
134+
`);
135+
136+
await elementUpdated(el);
137+
138+
const warning = consoleStub.getCall(0).args.at(0);
139+
const expectedContent =
140+
'The "label" attribute is required on <sp-clear-button>';
141+
142+
expect(consoleStub).to.be.calledOnce;
143+
expect(warning.includes(expectedContent)).to.be.true;
144+
});
145+
146+
it('should not log warning when label attribute is provided without slot content', async () => {
147+
const el = await fixture<ClearButton>(html`
148+
<sp-clear-button label="Clear"></sp-clear-button>
149+
`);
150+
151+
await elementUpdated(el);
152+
153+
expect(consoleStub).to.not.be.called;
154+
});
115155
});
116156
});

0 commit comments

Comments
 (0)