Skip to content

Commit 332b2c3

Browse files
committed
chore: safe lock of content
1 parent 2e12623 commit 332b2c3

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

src/Dialog/Content/Panel.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export interface PanelProps extends Omit<IDialogPropTypes, 'getOpenCount'> {
1313
onMouseDown?: React.MouseEventHandler;
1414
onMouseUp?: React.MouseEventHandler;
1515
holderRef?: React.Ref<HTMLDivElement>;
16+
/** Used for focus lock. When true and open, focus will lock into the panel */
17+
isFixedPos?: boolean;
1618
}
1719

1820
export type PanelRef = {
@@ -43,14 +45,15 @@ const Panel = React.forwardRef<PanelRef, PanelProps>((props, ref) => {
4345
height,
4446
classNames: modalClassNames,
4547
styles: modalStyles,
48+
isFixedPos,
4649
} = props;
4750

4851
// ================================= Refs =================================
4952
const { panel: panelRef } = React.useContext(RefContext);
5053
const internalRef = useRef<HTMLDivElement>(null);
5154
const mergedRef = useComposeRef(holderRef, panelRef, internalRef);
5255

53-
useLockFocus(visible, () => internalRef.current);
56+
useLockFocus(visible && isFixedPos, () => internalRef.current);
5457

5558
React.useImperativeHandle(ref, () => ({
5659
focus: () => {

src/Dialog/index.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const Dialog: React.FC<IDialogPropTypes> = (props) => {
5959
const contentRef = useRef<ContentRef>(null);
6060

6161
const [animatedVisible, setAnimatedVisible] = React.useState(visible);
62+
const [isFixedPos, setIsFixedPos] = React.useState(true);
6263

6364
// ========================== Init ==========================
6465
const ariaId = useId();
@@ -154,6 +155,12 @@ const Dialog: React.FC<IDialogPropTypes> = (props) => {
154155
if (visible) {
155156
setAnimatedVisible(true);
156157
saveLastOutSideActiveElementRef();
158+
159+
// Calc the position style
160+
if (wrapperRef.current) {
161+
const computedWrapStyle = getComputedStyle(wrapperRef.current);
162+
setIsFixedPos(computedWrapStyle.position === 'absolute');
163+
}
157164
} else if (
158165
animatedVisible &&
159166
contentRef.current.enableMotion() &&
@@ -203,6 +210,7 @@ const Dialog: React.FC<IDialogPropTypes> = (props) => {
203210
>
204211
<Content
205212
{...props}
213+
isFixedPos={isFixedPos}
206214
onMouseDown={onContentMouseDown}
207215
onMouseUp={onContentMouseUp}
208216
ref={contentRef}

0 commit comments

Comments
 (0)