|
1 | | -import { memo, useContext, useState } from "react"; |
| 1 | +import { memo, useContext, useState, useRef, useEffect } from "react"; |
2 | 2 | import { keyframes } from "@emotion/react"; |
3 | 3 | import styled from "@emotion/styled"; |
4 | 4 | import DxcActionIcon from "../action-icon/ActionIcon"; |
@@ -52,6 +52,10 @@ const Toast = styled.output<{ semantic: ToastPropsType["semantic"]; isClosing: b |
52 | 52 | @media (max-width: ${responsiveSizes.medium}rem) { |
53 | 53 | max-width: 100%; |
54 | 54 | } |
| 55 | +
|
| 56 | + &:focus { |
| 57 | + outline: none; |
| 58 | + } |
55 | 59 | `; |
56 | 60 |
|
57 | 61 | const ContentContainer = styled.div<{ loading: ToastPropsType["loading"]; semantic: ToastPropsType["semantic"] }>` |
@@ -88,24 +92,69 @@ const DxcToast = ({ |
88 | 92 | semantic, |
89 | 93 | }: ToastPropsType) => { |
90 | 94 | const [isClosing, setIsClosing] = useState(false); |
| 95 | + const toastRef = useRef<HTMLOutputElement>(null); |
| 96 | + const previouslyFocusedElement = useRef<HTMLElement | null>(null); |
91 | 97 | const translatedLabels = useContext(HalstackLanguageContext); |
92 | 98 |
|
93 | | - const clearClosingAnimationTimer = useTimeout( |
94 | | - () => { |
95 | | - setIsClosing(true); |
96 | | - }, |
97 | | - loading ? undefined : duration - 300 |
98 | | - ); |
| 99 | + // Timeouts |
| 100 | + const clearClosingAnimationTimer = useTimeout(() => setIsClosing(true), loading ? undefined : duration - 300); |
99 | 101 |
|
100 | | - const clearTimer = useTimeout( |
101 | | - () => { |
102 | | - onClear(); |
103 | | - }, |
104 | | - loading ? undefined : duration |
105 | | - ); |
| 102 | + const clearTimer = useTimeout(() => onClear(), loading ? undefined : duration); |
| 103 | + |
| 104 | + useEffect(() => { |
| 105 | + previouslyFocusedElement.current = document.activeElement as HTMLElement; |
| 106 | + |
| 107 | + toastRef.current?.focus(); |
| 108 | + |
| 109 | + return () => { |
| 110 | + previouslyFocusedElement.current?.focus?.(); |
| 111 | + }; |
| 112 | + }, []); |
| 113 | + |
| 114 | + const handleOnKeyDown = (event: React.KeyboardEvent<HTMLOutputElement>) => { |
| 115 | + if (event.key === "Tab") { |
| 116 | + event.preventDefault(); |
| 117 | + |
| 118 | + const focusableElements = toastRef.current?.querySelectorAll<HTMLElement>( |
| 119 | + 'button, [tabindex]:not([tabindex="-1"])' |
| 120 | + ); |
| 121 | + if (!focusableElements || focusableElements.length === 0) return; |
| 122 | + |
| 123 | + const firstElement = focusableElements?.[0]; |
| 124 | + const lastElement = focusableElements?.[focusableElements.length - 1]; |
| 125 | + const activeElement = document.activeElement; |
| 126 | + |
| 127 | + const elementsArray = Array.from(focusableElements); |
| 128 | + |
| 129 | + if (!event.shiftKey) { |
| 130 | + if (activeElement === lastElement) { |
| 131 | + previouslyFocusedElement.current?.focus?.(); |
| 132 | + } else { |
| 133 | + const currentIndex = elementsArray.indexOf(activeElement as HTMLElement); |
| 134 | + const nextElement = focusableElements[currentIndex + 1]; |
| 135 | + nextElement?.focus(); |
| 136 | + } |
| 137 | + } else { |
| 138 | + if (activeElement === firstElement) { |
| 139 | + previouslyFocusedElement.current?.focus?.(); |
| 140 | + } else { |
| 141 | + const currentIndex = elementsArray.indexOf(activeElement as HTMLElement); |
| 142 | + const prevElement = focusableElements[currentIndex - 1]; |
| 143 | + prevElement?.focus(); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + }; |
106 | 148 |
|
107 | 149 | return ( |
108 | | - <Toast isClosing={isClosing} role="status" semantic={semantic}> |
| 150 | + <Toast |
| 151 | + onKeyDown={handleOnKeyDown} |
| 152 | + isClosing={isClosing} |
| 153 | + role="status" |
| 154 | + semantic={semantic} |
| 155 | + tabIndex={-1} |
| 156 | + ref={toastRef} |
| 157 | + > |
109 | 158 | <ContentContainer loading={loading} semantic={semantic}> |
110 | 159 | <ToastIcon hideSemanticIcon={hideSemanticIcon} icon={icon} loading={loading} semantic={semantic} /> |
111 | 160 | <Message>{message}</Message> |
|
0 commit comments