Skip to content

Commit f30d6d8

Browse files
committed
fix: update focus methods across components for consistent behavior and improved focus management
1 parent 9cbe195 commit f30d6d8

File tree

23 files changed

+158
-86
lines changed

23 files changed

+158
-86
lines changed

packages/components/src/components/accordion/shadow.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import type {
1414
} from '../../schema';
1515
import { featureHint, validateAccordionCallbacks, validateDisabled, validateLabel, validateOpen } from '../../schema';
1616
import { nonce } from '../../utils/dev.utils';
17-
import { propagateFocus } from '../../utils/element-focus';
1817
import { dispatchDomEvent, KolEvent } from '../../utils/events';
1918
import { watchHeadingLevel } from '../heading/validation';
2019

@@ -53,8 +52,8 @@ export class KolAccordion implements AccordionAPI, FocusableElement {
5352
* Sets focus on the internal element.
5453
*/
5554
@Method()
56-
public async focus() {
57-
await propagateFocus(this.host, this.buttonWcRef);
55+
public async focus(): Promise<void> {
56+
await this.buttonWcRef?.focus(this.host as HTMLElement);
5857
}
5958

6059
private handleOnClick = (event: MouseEvent) => {

packages/components/src/components/badge/shadow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class KolBadge implements BadgeAPI, FocusableElement {
5656
*/
5757
@Method()
5858
public async focus(): Promise<void> {
59-
return Promise.resolve(this.smartButtonRef?.focus());
59+
return Promise.resolve(this.smartButtonRef?.focus(this.smartButtonRef as HTMLElement));
6060
}
6161

6262
public render(): JSX.Element {

packages/components/src/components/button-link/shadow.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import type {
1919
TooltipAlignPropType,
2020
VariantClassNamePropType,
2121
} from '../../schema';
22-
import { propagateFocus } from '../../utils/element-focus';
2322

2423
/**
2524
* The **ButtonLink** component is semantically a button but has the appearance of a link. All relevant properties of the Button component are adopted and extended with the design-defining properties of a link.
@@ -62,8 +61,8 @@ export class KolButtonLink implements ButtonLinkProps, FocusableElement {
6261
* Sets focus on the internal element.
6362
*/
6463
@Method()
65-
public async focus() {
66-
await propagateFocus(this.host, this.buttonWcRef);
64+
public async focus(): Promise<void> {
65+
await this.buttonWcRef?.focus(this.host as HTMLElement);
6766
}
6867

6968
public render(): JSX.Element {

packages/components/src/components/button/component.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import type {
1212
ButtonTypePropType,
1313
CustomClassPropType,
1414
DisabledPropType,
15-
FocusableElement,
1615
HideLabelPropType,
1716
IconsPropType,
1817
IdPropType,
@@ -68,7 +67,7 @@ import { AssociatedInputController } from '../input-adapter-leanup/associated.co
6867
tag: 'kol-button-wc',
6968
shadow: false,
7069
})
71-
export class KolButtonWc implements ButtonAPI, FocusableElement {
70+
export class KolButtonWc implements ButtonAPI {
7271
@Element() private readonly host?: HTMLKolButtonWcElement;
7372
private buttonRef?: HTMLButtonElement;
7473
private tooltipRef?: HTMLKolTooltipWcElement;
@@ -77,8 +76,8 @@ export class KolButtonWc implements ButtonAPI, FocusableElement {
7776
* Sets focus on the internal element.
7877
*/
7978
@Method()
80-
public async focus() {
81-
await propagateFocus(this.host, this.buttonRef);
79+
public async focus(host: HTMLElement): Promise<void> {
80+
await propagateFocus(host, this.buttonRef);
8281
}
8382

8483
private readonly hideTooltip = () => {
@@ -114,14 +113,14 @@ export class KolButtonWc implements ButtonAPI, FocusableElement {
114113
}
115114

116115
if (this.host) {
117-
dispatchDomEvent(this.host, KolEvent.click, this.state._value);
116+
dispatchDomEvent(this.host as HTMLElement, KolEvent.click, this.state._value);
118117
}
119118
};
120119

121120
private readonly onMouseDown = (event: MouseEvent) => {
122121
this.state?._on?.onMouseDown?.(event);
123122
if (this.host) {
124-
dispatchDomEvent(this.host, KolEvent.mousedown);
123+
dispatchDomEvent(this.host as HTMLElement, KolEvent.mousedown);
125124
}
126125
};
127126

@@ -316,7 +315,7 @@ export class KolButtonWc implements ButtonAPI, FocusableElement {
316315
};
317316

318317
public constructor() {
319-
this.controller = new AssociatedInputController(this, 'button', this.host);
318+
this.controller = new AssociatedInputController(this, 'button', this.host as HTMLElement);
320319
}
321320

322321
@Watch('_accessKey')

packages/components/src/components/button/shadow.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import type {
1919
SyncValueBySelectorPropType,
2020
TooltipAlignPropType,
2121
} from '../../schema';
22-
import { propagateFocus } from '../../utils/element-focus';
2322

2423
/**
2524
* The **Button** component is used to present users with action options and arrange them in a clear hierarchy. It helps users find the most important actions on a page or within a viewport and allows them to execute those actions. The button label clearly indicates which action will be triggered. Buttons allow users to confirm a change, complete steps in a task, or make decisions.
@@ -54,8 +53,8 @@ export class KolButton implements ButtonProps, FocusableElement {
5453
* Sets focus on the internal element.
5554
*/
5655
@Method()
57-
public async focus() {
58-
await propagateFocus(this.host, this.buttonWcRef);
56+
public async focus(): Promise<void> {
57+
await this.buttonWcRef?.focus(this.host as HTMLElement);
5958
}
6059

6160
public render(): JSX.Element {

packages/components/src/components/details/shadow.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import KolCollapsibleFc, { type CollapsibleProps } from '../../functional-compon
33
import type { DetailsAPI, DetailsCallbacksPropType, DetailsStates, DisabledPropType, FocusableElement, HeadingLevel, LabelPropType } from '../../schema';
44
import { validateDetailsCallbacks, validateDisabled, validateLabel, validateOpen } from '../../schema';
55
import { nonce } from '../../utils/dev.utils';
6-
import { propagateFocus } from '../../utils/element-focus';
76
import { dispatchDomEvent, KolEvent } from '../../utils/events';
87
import { watchHeadingLevel } from '../heading/validation';
98

@@ -39,8 +38,8 @@ export class KolDetails implements DetailsAPI, FocusableElement {
3938
* Sets focus on the internal element.
4039
*/
4140
@Method()
42-
public async focus() {
43-
await propagateFocus(this.host, this.buttonWcRef);
41+
public async focus(): Promise<void> {
42+
await this.buttonWcRef?.focus(this.host as HTMLElement);
4443
}
4544

4645
private toggleTimeout?: ReturnType<typeof setTimeout>;

packages/components/src/components/form/shadow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class KolForm implements FormAPI {
6161

6262
private readonly setBlockElement = (el?: HTMLElement) => (this.errorListBlock = el);
6363

64-
private readonly setFirstLinkElement = (el?: HTMLElement) => (this.errorListFirstLink = el);
64+
private readonly setFirstLinkElement = (el?: HTMLKolLinkWcElement) => (this.errorListFirstLink = el as HTMLElement);
6565

6666
private renderErrorList(errorList?: ErrorListPropType[]): JSX.Element {
6767
return (

packages/components/src/components/link-button/shadow.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import type {
1919
ShortKeyPropType,
2020
TooltipAlignPropType,
2121
} from '../../schema';
22-
import { propagateFocus } from '../../utils/element-focus';
2322

2423
/**
2524
* The **LinkButton** component is semantically a link but has the appearance of a button. All relevant properties of the Link component are adopted and extended with the design-defining properties of a button.
@@ -45,8 +44,8 @@ export class KolLinkButton implements LinkButtonProps, FocusableElement {
4544
* Sets focus on the internal element.
4645
*/
4746
@Method()
48-
public async focus() {
49-
await propagateFocus(this.host, this.linkWcRef);
47+
public async focus(): Promise<void> {
48+
await this.linkWcRef?.focus(this.host as HTMLElement);
5049
}
5150

5251
public render(): JSX.Element {

packages/components/src/components/link/component.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import type {
1212
CustomClassPropType,
1313
DisabledPropType,
1414
DownloadPropType,
15-
FocusableElement,
1615
HideLabelPropType,
1716
HrefPropType,
1817
InlinePropType,
@@ -70,7 +69,7 @@ import clsx from '../../utils/clsx';
7069
tag: 'kol-link-wc',
7170
shadow: false,
7271
})
73-
export class KolLinkWc implements InternalLinkAPI, FocusableElement {
72+
export class KolLinkWc implements InternalLinkAPI {
7473
@Element() private readonly host?: HTMLKolLinkElement;
7574

7675
private anchorRef?: HTMLAnchorElement;
@@ -91,8 +90,8 @@ export class KolLinkWc implements InternalLinkAPI, FocusableElement {
9190
* Sets focus on the internal element.
9291
*/
9392
@Method()
94-
public async focus() {
95-
await propagateFocus(this.host, this.anchorRef);
93+
public async focus(host: HTMLElement): Promise<void> {
94+
await propagateFocus(host, this.anchorRef);
9695
}
9796

9897
private readonly onClick = (event: Event) => {

packages/components/src/components/link/shadow.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import type {
2020
TooltipAlignPropType,
2121
VariantClassNamePropType,
2222
} from '../../schema';
23-
import { propagateFocus } from '../../utils/element-focus';
2423

2524
@Component({
2625
tag: 'kol-link',
@@ -41,8 +40,8 @@ export class KolLink implements LinkProps, FocusableElement {
4140
* Sets focus on the internal element.
4241
*/
4342
@Method()
44-
public async focus() {
45-
await propagateFocus(this.host, this.linkWcRef);
43+
public async focus(): Promise<void> {
44+
await this.linkWcRef?.focus(this.host as HTMLElement);
4645
}
4746

4847
public render(): JSX.Element {

0 commit comments

Comments
 (0)