Skip to content

Commit 63c7d1d

Browse files
committed
fix: ensure host element is defined before focusing in multiple components
1 parent 39e7a5a commit 63c7d1d

File tree

20 files changed

+67
-36
lines changed

20 files changed

+67
-36
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ export class KolAccordion implements AccordionAPI, FocusableElement {
5353
*/
5454
@Method()
5555
public async focus(): Promise<void> {
56-
await this.buttonWcRef?.focus(this.host as HTMLElement);
56+
if (this.host) {
57+
await this.buttonWcRef?.focus(this.host);
58+
}
5759
}
5860

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

packages/components/src/components/alert/component.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class KolAlertWc implements AlertAPI {
2929
private readonly close = () => {
3030
this._on?.onClose?.(new Event('Close'));
3131
if (this.host) {
32-
dispatchDomEvent(this.host as HTMLElement, KolEvent.close);
32+
dispatchDomEvent(this.host, KolEvent.close);
3333
}
3434
};
3535

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ featureHint(`[KolBadge] Optimierung des _color-Properties (rgba, rgb, hex usw.).
2222
shadow: true,
2323
})
2424
export class KolBadge implements BadgeAPI, FocusableElement {
25-
@Element() private readonly host!: HTMLKolBadgeElement;
25+
@Element() private readonly host?: HTMLKolBadgeElement;
2626
private bgColorStr = '#000';
2727
private colorStr = '#fff';
2828
private readonly id = nonce();
@@ -57,7 +57,9 @@ export class KolBadge implements BadgeAPI, FocusableElement {
5757
*/
5858
@Method()
5959
public async focus(): Promise<void> {
60-
return this.smartButtonRef?.focus(this.host);
60+
if (this.host) {
61+
return this.smartButtonRef?.focus(this.host);
62+
}
6163
}
6264

6365
public render(): JSX.Element {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ export class KolButtonLink implements ButtonLinkProps, FocusableElement {
6262
*/
6363
@Method()
6464
public async focus(): Promise<void> {
65-
await this.buttonWcRef?.focus(this.host as HTMLElement);
65+
if (this.host) {
66+
await this.buttonWcRef?.focus(this.host);
67+
}
6668
}
6769

6870
public render(): JSX.Element {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ export class KolButton implements ButtonProps, FocusableElement {
5454
*/
5555
@Method()
5656
public async focus(): Promise<void> {
57-
await this.buttonWcRef?.focus(this.host as HTMLElement);
57+
if (this.host) {
58+
await this.buttonWcRef?.focus(this.host);
59+
}
5860
}
5961

6062
public render(): JSX.Element {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ export class KolDetails implements DetailsAPI, FocusableElement {
3939
*/
4040
@Method()
4141
public async focus(): Promise<void> {
42-
await this.buttonWcRef?.focus(this.host as HTMLElement);
42+
if (this.host) {
43+
await this.buttonWcRef?.focus(this.host);
44+
}
4345
}
4446

4547
private toggleTimeout?: ReturnType<typeof setTimeout>;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { dispatchDomEvent, KolEvent } from '../../utils/events';
2222
shadow: true,
2323
})
2424
export class KolForm implements FormAPI {
25-
@Element() private readonly host?: HTMLKolTextareaElement;
25+
@Element() private readonly host?: HTMLKolFormElement;
2626
errorListBlock?: HTMLElement;
2727
errorListFirstLink?: HTMLElement;
2828
private readonly translateErrorListMessage = translate('kol-error-list-message');
@@ -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?: HTMLKolLinkWcElement) => (this.errorListFirstLink = el as HTMLElement);
64+
private readonly setFirstLinkElement = (el?: HTMLKolLinkWcElement) => (this.errorListFirstLink = el as HTMLElement | undefined);
6565

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

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ export class KolLinkButton implements LinkButtonProps, FocusableElement {
4545
*/
4646
@Method()
4747
public async focus(): Promise<void> {
48-
await this.linkWcRef?.focus(this.host as HTMLElement);
48+
if (this.host) {
49+
await this.linkWcRef?.focus(this.host);
50+
}
4951
}
5052

5153
public render(): JSX.Element {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ export class KolLink implements LinkProps, FocusableElement {
4141
*/
4242
@Method()
4343
public async focus(): Promise<void> {
44-
await this.linkWcRef?.focus(this.host as HTMLElement);
44+
if (this.host) {
45+
await this.linkWcRef?.focus(this.host);
46+
}
4547
}
4648

4749
public render(): JSX.Element {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { autoUpdate } from '@floating-ui/dom';
22
import type { JSX } from '@stencil/core';
3-
import { Component, Element, h, Method, Prop, State, Watch } from '@stencil/core';
3+
import { Component, h, Method, Prop, State, Watch } from '@stencil/core';
44
import { KolButtonWcTag } from '../../core/component-names';
55
import type {
66
AccessKeyPropType,
@@ -34,8 +34,6 @@ import clsx from '../../utils/clsx';
3434
})
3535
// class implementing PopoverButtonProps and not API because we don't want to repeat the entire state and validation for button props
3636
export class KolPopoverButtonWc implements PopoverButtonProps {
37-
@Element() host!: HTMLElement;
38-
3937
private refButton?: HTMLKolButtonWcElement;
4038
private refPopover?: HTMLDivElement;
4139
private cleanupAutoPositioning?: () => void;

0 commit comments

Comments
 (0)