Skip to content

Commit

Permalink
feat(Modal): autofocus (#2749)
Browse files Browse the repository at this point in the history
  • Loading branch information
mainframev committed Feb 21, 2021
1 parent c7f71b5 commit 7318569
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/orbit-components/src/Modal/Modal.stories.js
Expand Up @@ -172,15 +172,15 @@ storiesOf("Modal", module)
() => {
const showMore = boolean(false);
return (
<Modal onClose={action("onClose")} fixedFooter>
<Modal onClose={action("onClose")} fixedFooter autoFocus={false}>
<ModalHeader title="Refund" description="Reservation number: 123456789" />
<ModalSection>
<Stack>
<Card title="Cancellation" icon={<Airplane />} />
<Text size="small" weight="bold">
Contact information
</Text>
<InputField label="E-mail" placeholder="Your email" />
<InputField label="E-mail" autoFocus placeholder="Your email" />
<InputGroup
flex={["0 0 120px", "1 1 100%"]}
onChange={action("onChange")}
Expand Down
1 change: 1 addition & 0 deletions packages/orbit-components/src/Modal/README.md
Expand Up @@ -33,6 +33,7 @@ Table below contains all types of the props available in the Modal component.
| onClose | `event => void \| Promise` | | Function for handling onClose event. If you don't pass any function the Close button will not be displayed and it will not be possible to close the Modal. [See Functional specs](#functional-specs) |
| preventOverlayClose | `boolean` | | Property for preventing closing of modal when there is a action on overlay. BEWARE: This should be used only in very specials edge-cases! It breaks user experience. |
| hasCloseButton | `boolean` | `true` | Defines whether the Modal displays a close button. If you disable this, we recommend adding some kind of an alternative. |
| autoFocus | `boolean` | `true` | The autofocus attribute of the Modal, see [this docs](https://www.w3schools.com/tags/att_autofocus.asp). |

### Modal enum

Expand Down
1 change: 1 addition & 0 deletions packages/orbit-components/src/Modal/index.d.ts
Expand Up @@ -13,6 +13,7 @@ export interface Props extends Common.Global {
readonly size?: Size;
readonly children: React.ReactNode;
readonly scrollingElementRef?: React.Ref<HTMLElement>;
readonly autoFocus?: boolean;
readonly onClose?: Common.Event<
React.KeyboardEvent<HTMLDivElement> | React.SyntheticEvent<HTMLButtonElement> | React.MouseEvent
>;
Expand Down
4 changes: 3 additions & 1 deletion packages/orbit-components/src/Modal/index.js
Expand Up @@ -332,6 +332,7 @@ const Modal = React.forwardRef<Props, Instance>(
scrollingElementRef,
children,
onClose,
autoFocus = true,
fixedFooter = false,
isMobileFullPage = false,
preventOverlayClose = false,
Expand Down Expand Up @@ -407,7 +408,7 @@ const Modal = React.forwardRef<Props, Instance>(
};

const setFirstFocus = () => {
if (modalBody.current) {
if (modalBody.current && autoFocus) {
modalBody.current.focus();
}
};
Expand Down Expand Up @@ -618,6 +619,7 @@ const Modal = React.forwardRef<Props, Instance>(
data-test={dataTest}
ref={modalBodyRef}
role="dialog"
autoFocus={autoFocus}
aria-modal="true"
aria-labelledby={modalID}
>
Expand Down
1 change: 1 addition & 0 deletions packages/orbit-components/src/Modal/index.js.flow
Expand Up @@ -28,6 +28,7 @@ export type onClose = (
export type Props = {|
+size?: Size,
+children: React.Node,
+autoFocus?: boolean,
+scrollingElementRef?:
| ((instance: HTMLElement | null) => void)
| {| current: HTMLElement | null |},
Expand Down

0 comments on commit 7318569

Please sign in to comment.