Skip to content

Commit 0b4ffbb

Browse files
authored
Merge pull request #999 from devtron-labs/fix/date-picker-focus
fix: date picker focus
2 parents d0712be + 46999df commit 0b4ffbb

12 files changed

Lines changed: 44 additions & 70 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "1.22.3",
3+
"version": "1.22.4",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Common/Drawer/Drawer.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { VisibleModal } from '../Modals/VisibleModal'
1919
import './Drawer.scss'
2020
import { DTFocusTrapType } from '@Shared/Components/DTFocusTrap'
2121

22-
export interface DrawerProps extends Pick<DTFocusTrapType, 'initialFocus'> {
22+
export interface DrawerProps extends Pick<DTFocusTrapType, 'initialFocus' | 'avoidFocusTrap' > {
2323
position: 'left' | 'right' | 'bottom' | 'top'
2424
children?: any
2525
backdrop?: boolean
@@ -45,6 +45,7 @@ export const Drawer = ({
4545
onClose,
4646
disableTransition,
4747
initialFocus = undefined,
48+
avoidFocusTrap = false,
4849
}: DrawerProps) => {
4950
const drawerRef = useRef(null)
5051
useEffect(() => {
@@ -69,6 +70,7 @@ export const Drawer = ({
6970
onEscape={onEscape}
7071
close={onClose}
7172
initialFocus={initialFocus}
73+
avoidFocusTrap={avoidFocusTrap}
7274
>
7375
<aside
7476
style={style}

src/Common/Modals/VisibleModal.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export class VisibleModal extends React.Component<{
2727
close?: (e?) => void
2828
onEscape?: (e?) => void
2929
initialFocus?: DTFocusTrapType['initialFocus']
30+
avoidFocusTrap?: boolean
3031
}> {
3132
constructor(props) {
3233
super(props)
@@ -56,6 +57,7 @@ export class VisibleModal extends React.Component<{
5657
onEscape={this.escFunction}
5758
onClick={this.handleBodyClick}
5859
initialFocus={this.props.initialFocus ?? undefined}
60+
avoidFocusTrap={this.props.avoidFocusTrap}
5961
>
6062
<div className={this.props.parentClassName}>
6163
<div className={`visible-modal__body ${this.props.className || ''}`}>{this.props.children}</div>

src/Shared/Components/Backdrop/Backdrop.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const Backdrop = ({
3333
deactivateFocusOnEscape = true,
3434
initialFocus,
3535
returnFocusOnDeactivate,
36+
avoidFocusTrap,
3637
}: BackdropProps) => {
3738
// STATES
3839
const [portalContainer, setPortalContainer] = useState<HTMLElement | null>(null)
@@ -92,6 +93,7 @@ const Backdrop = ({
9293
deactivateFocusOnEscape={deactivateFocusOnEscape}
9394
initialFocus={initialFocus ?? undefined}
9495
returnFocusOnDeactivate={returnFocusOnDeactivate}
96+
avoidFocusTrap={avoidFocusTrap}
9597
>
9698
<motion.div
9799
initial={{ opacity: 0 }}

src/Shared/Components/Backdrop/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import { MouseEvent, ReactNode } from 'react'
1919
import { DTFocusTrapType } from '../DTFocusTrap'
2020

2121
export interface BackdropProps
22-
extends Pick<DTFocusTrapType, 'deactivateFocusOnEscape' | 'initialFocus' | 'onEscape' | 'returnFocusOnDeactivate'> {
22+
extends Pick<
23+
DTFocusTrapType,
24+
'deactivateFocusOnEscape' | 'initialFocus' | 'onEscape' | 'returnFocusOnDeactivate' | 'avoidFocusTrap'
25+
> {
2326
/**
2427
* The content to be rendered within the backdrop component.
2528
*/

src/Shared/Components/ConfirmationModal/ConfirmationModal.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const ConfirmationModalBody = ({
4646
shouldCloseOnEscape = true,
4747
isLandscapeView = false,
4848
showConfetti = false,
49+
avoidFocusTrap = false,
4950
}: ConfirmationModalBodyProps) => {
5051
const { registerShortcut, unregisterShortcut } = useRegisterShortcut()
5152

@@ -92,6 +93,7 @@ const ConfirmationModalBody = ({
9293
deactivateFocusOnEscape={shouldCloseOnEscape}
9394
// Since when custom input is present, we auto focus on input, else focus on primary button
9495
initialFocus={confirmationConfig ? false : `#${PRIMARY_BUTTON_ID}`}
96+
avoidFocusTrap={avoidFocusTrap}
9597
>
9698
<motion.div
9799
className={`${isLandscapeView ? 'w-500' : 'w-400'} confirmation-modal border__secondary flexbox-col br-8 bg__primary dc__m-auto mt-40`}

src/Shared/Components/ConfirmationModal/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export type ConfirmationModalProps<isConfig extends boolean = false> = PropsWith
104104
* @default false
105105
*/
106106
showConfetti?: boolean
107+
avoidFocusTrap?: boolean
107108
}> &
108109
ButtonConfigAndVariantType<isConfig> &
109110
(isConfig extends false

src/Shared/Components/DTFocusTrap/DTFocusTrap.tsx

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,22 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'
17+
import { useCallback, useEffect } from 'react'
1818
import FocusTrap from 'focus-trap-react'
1919

20-
import { noop } from '@Common/Helper'
2120
import { ALLOW_ACTION_OUTSIDE_FOCUS_TRAP } from '@Shared/constants'
2221
import { preventBodyScroll } from '@Shared/Helpers'
2322

2423
import { DTFocusTrapType } from './types'
2524

26-
const FocusTrapControlContext = createContext<{
27-
disableFocusTrap: () => void
28-
resumeFocusTrap: () => void
29-
}>(null)
30-
31-
export const useFocusTrapControl = () => {
32-
const context = useContext(FocusTrapControlContext)
33-
if (!context) {
34-
return {
35-
disableFocusTrap: noop,
36-
resumeFocusTrap: noop,
37-
}
38-
}
39-
return context
40-
}
41-
4225
const DTFocusTrap = ({
4326
onEscape,
4427
deactivateFocusOnEscape = true,
4528
children,
4629
initialFocus = undefined,
4730
returnFocusOnDeactivate = true,
31+
avoidFocusTrap = false,
4832
}: DTFocusTrapType) => {
49-
const [isFocusEnabled, setIsFocusEnabled] = useState(true)
50-
5133
const handleEscape = useCallback(
5234
(e?: KeyboardEvent | MouseEvent) => {
5335
onEscape(e)
@@ -64,40 +46,30 @@ const DTFocusTrap = ({
6446
}
6547
}, [])
6648

67-
const focusContextValue = useMemo(
68-
() => ({
69-
disableFocusTrap: () => setIsFocusEnabled(false),
70-
resumeFocusTrap: () => setIsFocusEnabled(true),
71-
}),
72-
[],
73-
)
74-
7549
return (
76-
<FocusTrapControlContext.Provider value={focusContextValue}>
77-
<FocusTrap
78-
active={isFocusEnabled}
79-
focusTrapOptions={{
80-
escapeDeactivates: handleEscape,
81-
initialFocus,
82-
allowOutsideClick: (event) => {
83-
// Allow up to 3 parent levels to check for the allowed class
84-
let el = event.target as Element | null
85-
let depth = 0
86-
while (el && depth < 4) {
87-
if (el.classList && el.classList.contains(ALLOW_ACTION_OUTSIDE_FOCUS_TRAP)) {
88-
return true
89-
}
90-
el = el.parentElement
91-
depth += 1
50+
<FocusTrap
51+
active={!avoidFocusTrap}
52+
focusTrapOptions={{
53+
escapeDeactivates: handleEscape,
54+
initialFocus,
55+
allowOutsideClick: (event) => {
56+
// Allow up to 3 parent levels to check for the allowed class
57+
let el = event.target as Element | null
58+
let depth = 0
59+
while (el && depth < 4) {
60+
if (el.classList && el.classList.contains(ALLOW_ACTION_OUTSIDE_FOCUS_TRAP)) {
61+
return true
9262
}
93-
return false
94-
},
95-
returnFocusOnDeactivate,
96-
}}
97-
>
98-
{children}
99-
</FocusTrap>
100-
</FocusTrapControlContext.Provider>
63+
el = el.parentElement
64+
depth += 1
65+
}
66+
return false
67+
},
68+
returnFocusOnDeactivate,
69+
}}
70+
>
71+
{children}
72+
</FocusTrap>
10173
)
10274
}
10375

src/Shared/Components/DTFocusTrap/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
* limitations under the License.
1515
*/
1616

17-
export { default as DTFocusTrap, useFocusTrapControl } from './DTFocusTrap'
17+
export { default as DTFocusTrap } from './DTFocusTrap'
1818
export type { DTFocusTrapType } from './types'

0 commit comments

Comments
 (0)