Skip to content

Commit 1b8c8f2

Browse files
authored
Merge pull request #741 from lumapps/fix/lightbox-parent-focus
fix(lightbox): fix wrong focus parent behavior
2 parents f7f6fce + a60f1c6 commit 1b8c8f2

File tree

3 files changed

+95
-4
lines changed

3 files changed

+95
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Fixed generated typescript types for NPM publication.
1313
- Fixed release script version and changelog update.
14+
- Lightbox : Focus parent element only when his lightbox was previously opened.
1415

1516
## [2.1.5][] - 2021-11-30
1617

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import React from 'react';
2+
import {
3+
ImageBlock,
4+
Slideshow,
5+
SlideshowItem,
6+
Toolbar,
7+
Divider,
8+
Alignment,
9+
TextField,
10+
Typography,
11+
Link,
12+
Lightbox,
13+
ThumbnailProps,
14+
} from '@lumx/react';
15+
import { thumbnailsKnob } from '@lumx/react/stories/knobs/thumbnailsKnob';
16+
17+
export default { title: 'LumX components/lightbox/Lightbox' };
18+
19+
interface RowItemProps {
20+
image: ThumbnailProps;
21+
}
22+
23+
const RowItem: React.FC<RowItemProps> = ({ image }) => {
24+
const [isOpen, setOpen] = React.useState(false);
25+
const { image: url, alt: name } = image;
26+
const linkRef = React.useRef(null);
27+
28+
return (
29+
<>
30+
<Toolbar
31+
label={
32+
<Link ref={linkRef} typography={Typography.subtitle1} onClick={() => setOpen(true)}>
33+
{name}
34+
</Link>
35+
}
36+
/>
37+
<Lightbox
38+
closeButtonProps={{ label: 'Close' }}
39+
isOpen={isOpen}
40+
onClose={() => setOpen(false)}
41+
parentElement={linkRef}
42+
>
43+
<Slideshow
44+
activeIndex={0}
45+
fillHeight
46+
slideshowControlsProps={{
47+
nextButtonProps: { label: 'Next' },
48+
previousButtonProps: { label: 'Previous' },
49+
}}
50+
>
51+
<SlideshowItem key={name}>
52+
<ImageBlock align={Alignment.center} alt={name} fillHeight image={url} />
53+
</SlideshowItem>
54+
</Slideshow>
55+
</Lightbox>
56+
</>
57+
);
58+
};
59+
60+
export const Focus = () => {
61+
const [textFieldValue, setTextFieldValue] = React.useState('');
62+
const images: ThumbnailProps[] = thumbnailsKnob(12);
63+
64+
return (
65+
<>
66+
<TextField
67+
value={textFieldValue}
68+
onChange={setTextFieldValue}
69+
className="lumx-spacing-margin-vertical-big"
70+
/>
71+
{images.map((image, index) => {
72+
const itemPosition = index + 1;
73+
return (
74+
<div key={itemPosition}>
75+
<RowItem image={image} />
76+
<Divider />
77+
</div>
78+
);
79+
})}
80+
</>
81+
);
82+
};

packages/lumx-react/src/components/lightbox/Lightbox.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { Comp, GenericProps, getRootClassName, handleBasicClasses } from '@lumx/
1010

1111
import { useFocusTrap } from '@lumx/react/hooks/useFocusTrap';
1212
import { useDelayedVisibility } from '@lumx/react/hooks/useDelayedVisibility';
13-
import { useFocus } from '@lumx/react/hooks/useFocus';
1413
import { useDisableBodyScroll } from '@lumx/react/hooks/useDisableBodyScroll';
1514
import { ClickAwayProvider } from '@lumx/react/utils/ClickAwayProvider';
1615
import { mergeRefs } from '@lumx/react/utils/mergeRefs';
@@ -90,9 +89,18 @@ export const Lightbox: Comp<LightboxProps, HTMLDivElement> = forwardRef((props,
9089
// eslint-disable-next-line react-hooks/rules-of-hooks
9190
useFocusTrap(wrapperRef.current, childrenRef.current?.firstChild);
9291

93-
// Focus the parent element on close.
94-
// eslint-disable-next-line react-hooks/rules-of-hooks
95-
useFocus(parentElement?.current, !isOpen);
92+
const previousOpen = React.useRef(isOpen);
93+
94+
React.useEffect(() => {
95+
if (isOpen !== previousOpen.current) {
96+
previousOpen.current = isOpen;
97+
98+
// Focus the parent element on close.
99+
if (!isOpen && parentElement && parentElement.current) {
100+
parentElement.current.focus();
101+
}
102+
}
103+
}, [isOpen, parentElement]);
96104

97105
// Close lightbox on escape key pressed.
98106
// eslint-disable-next-line react-hooks/rules-of-hooks

0 commit comments

Comments
 (0)