Skip to content

Commit

Permalink
refactor(core/button): imporved autofocus handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nuke-ellington committed Dec 2, 2024
1 parent af8571b commit fb06cd9
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ import { IxActiveModal } from '@siemens/ix-angular';
>
Cancel
</ix-button>
<ix-button class="close-modal" (click)="activeModal.close('okay')">
<ix-button
autofocus
class="close-modal"
(click)="activeModal.close('okay')"
>
OK
</ix-button>
</ix-modal-footer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default class ModalByInstance {
await this.modalService.open({
content: ModalByInstanceExample,
data: 'Some data',
autofocus: true,
});
}
}
4 changes: 2 additions & 2 deletions packages/angular/src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,14 @@ export declare interface IxBreadcrumbItem extends Components.IxBreadcrumbItem {}


@ProxyCmp({
inputs: ['autofocused', 'disabled', 'ghost', 'icon', 'loading', 'outline', 'type', 'variant']
inputs: ['disabled', 'ghost', 'icon', 'loading', 'outline', 'type', 'variant']
})
@Component({
selector: 'ix-button',
changeDetection: ChangeDetectionStrategy.OnPush,
template: '<ng-content></ng-content>',
// eslint-disable-next-line @angular-eslint/no-inputs-metadata-property
inputs: ['autofocused', 'disabled', 'ghost', 'icon', 'loading', 'outline', 'type', 'variant'],
inputs: ['disabled', 'ghost', 'icon', 'loading', 'outline', 'type', 'variant'],
})
export class IxButton {
protected el: HTMLElement;
Expand Down
22 changes: 0 additions & 22 deletions packages/core/component-doc.json
Original file line number Diff line number Diff line change
Expand Up @@ -1340,28 +1340,6 @@
]
},
"props": [
{
"name": "autofocused",
"type": "boolean",
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"mutable": false,
"attr": "autofocused",
"reflectToAttr": false,
"docs": "Autofocus the button",
"docsTags": [],
"default": "false",
"values": [
{
"type": "boolean"
}
],
"optional": false,
"required": false
},
{
"name": "disabled",
"type": "boolean",
Expand Down
9 changes: 0 additions & 9 deletions packages/core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,6 @@ export namespace Components {
}
interface IxButton {
"alignment": 'center' | 'start';
/**
* Autofocus the button
*/
"autofocused": boolean;
/**
* Disable the button
*/
Expand All @@ -293,7 +289,6 @@ export namespace Components {
* Outline button
*/
"outline": boolean;
"setFocus": (delay?: number) => Promise<void>;
/**
* Type of the button
*/
Expand Down Expand Up @@ -5283,10 +5278,6 @@ declare namespace LocalJSX {
}
interface IxButton {
"alignment"?: 'center' | 'start';
/**
* Autofocus the button
*/
"autofocused"?: boolean;
/**
* Disable the button
*/
Expand Down
28 changes: 7 additions & 21 deletions packages/core/src/components/button/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {
Component,
Element,
h,
Host,
Listen,
Method,
Prop,
} from '@stencil/core';
import { Component, Element, h, Host, Listen, Prop } from '@stencil/core';
import { BaseButton, BaseButtonProps } from './base-button';

export type ButtonVariant = 'danger' | 'primary' | 'secondary';
Expand Down Expand Up @@ -64,11 +56,6 @@ export class Button {
*/
@Prop() icon?: string;

/**
* Autofocus the button
*/
@Prop() autofocused = false;

/** @internal */
@Prop() alignment: 'center' | 'start' = 'center';

Expand Down Expand Up @@ -113,12 +100,8 @@ export class Button {
}
}

/** @internal */
@Method()
async setFocus(delay = 0) {
setTimeout(() => {
this.hostElement.shadowRoot!.querySelector('button')?.focus();
}, delay);
setFocus() {
this.hostElement.shadowRoot!.querySelector('button')?.focus();
}

render() {
Expand All @@ -136,14 +119,17 @@ export class Button {
onClick: () => this.dispatchFormEvents(),
type: this.type,
alignment: this.alignment,
autofocus: this.autofocused,
autofocus: this.hostElement.autofocus,
tabIndex: this.hostElement.tabIndex,
};

return (
<Host
tabindex={this.disabled ? -1 : 0}
class={{
disabled: this.disabled || this.loading,
}}
onFocus={() => this.setFocus()}
>
<BaseButton {...baseButtonProps}>
<slot></slot>
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/components/modal/test/modal.ct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,11 @@ test('button receives focus on load', async ({ mount, page }) => {
elm.innerHTML = `
<ix-modal-header>Title</ix-modal-header>
<ix-modal-footer>
<ix-button autofocused>OK</ix-button>
<ix-button autofocus>OK</ix-button>
</ix-modal-footer>
`;
window.showModal({
content: elm,
autofocus: true,
});
const okButton = elm.querySelector('ix-button');
okButton?.addEventListener('click', () => {
Expand Down
11 changes: 6 additions & 5 deletions packages/core/src/components/utils/modal/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,12 @@ export async function showModal<T>(
}
);

if (config.autofocus) {
const autofocusElement = dialogRef.querySelector('ix-button[autofocused]');
if (autofocusElement) {
(autofocusElement as HTMLIxButtonElement).setFocus(150);
}
const autofocusElement = dialogRef.querySelector('ix-button[autofocus]');

if (autofocusElement) {
requestAnimationFrame(() =>
(autofocusElement as HTMLIxButtonElement).focus()
);
}

return {
Expand Down
4 changes: 2 additions & 2 deletions packages/storybook-docs/src/stories/modal.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const ShowFunction: Story = {
<ix-modal-content>Content</ix-modal-content>
<ix-modal-footer>
<ix-button outline>Close</ix-button>
<ix-button autofocused>Okay</ix-button>
<ix-button autofocus>Okay</ix-button>
</ix-modal-footer>
`,
mount
Expand All @@ -82,7 +82,7 @@ export const ShowFunction: Story = {
p.onDismiss.once(() => refs.delete(ctx.id));
});
mount
.querySelector('ix-button[autofocused]')
.querySelector('ix-button[autofocus]')
?.addEventListener('click', () => dismissModal(mount));
refs.set(ctx.id, mount);
}
Expand Down
1 change: 0 additions & 1 deletion packages/vue/src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ export const IxButton = /*@__PURE__*/ defineContainer<JSX.IxButton>('ix-button',
'type',
'loading',
'icon',
'autofocused',
'alignment',
'iconSize'
]);
Expand Down

0 comments on commit fb06cd9

Please sign in to comment.