Skip to content

Commit 1afc220

Browse files
authored
fix(Gallery): hide navigation in single item mode (#276)
1 parent 6639b0c commit 1afc220

File tree

2 files changed

+58
-24
lines changed

2 files changed

+58
-24
lines changed

src/components/Gallery/Gallery.tsx

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const emptyItems: GalleryItemProps[] = [];
2323

2424
export type GalleryProps = {
2525
className?: string;
26-
children?: React.ReactElement<GalleryItemProps>[];
26+
children?: React.ReactElement<GalleryItemProps>[] | React.ReactElement<GalleryItemProps>;
2727
emptyMessage?: string;
2828
} & Pick<ModalProps, 'open' | 'container' | 'onOpenChange'> &
2929
Pick<UseNavigationProps, 'initialItemIndex'>;
@@ -79,6 +79,8 @@ export const Gallery = ({
7979

8080
const activeItem = items[activeItemIndex] || items[0];
8181

82+
const withNavigation = items.length > 1;
83+
8284
return (
8385
<Modal
8486
container={container}
@@ -89,7 +91,7 @@ export const Gallery = ({
8991
<div className={cnGallery('content')}>
9092
<div className={cnGallery('header')}>
9193
<div className={cnGallery('active-item-info')}>{activeItem?.name}</div>
92-
{items.length > 0 && (
94+
{withNavigation && (
9395
<div className={cnGallery('navigation')}>
9496
<Button size="l" view="flat" onClick={handleGoToPrevious}>
9597
<Icon data={direction === 'rtl' ? ArrowRight : ArrowLeft} />
@@ -151,7 +153,7 @@ export const Gallery = ({
151153
</GalleryFallbackText>
152154
)}
153155
{activeItem?.view}
154-
{activeItem && !activeItem.interactive && (
156+
{withNavigation && activeItem && !activeItem.interactive && (
155157
<React.Fragment>
156158
<NavigationButton onClick={handleGoToPrevious} position="start" />
157159
<NavigationButton onClick={handleGoToNext} position="end" />
@@ -160,27 +162,29 @@ export const Gallery = ({
160162
</div>
161163
{!fullScreen && (
162164
<div className={cnGallery('footer')}>
163-
<div className={cnGallery('preview-list')}>
164-
{items.map((item, index) => {
165-
const handleClick = () => {
166-
setActiveItemIndex(index);
167-
};
168-
169-
const selected = activeItemIndex === index;
170-
171-
return (
172-
<button
173-
ref={itemRefs[index]}
174-
type="button"
175-
key={index}
176-
onClick={handleClick}
177-
className={cnGallery('preview-list-item', {selected})}
178-
>
179-
{item.thumbnail}
180-
</button>
181-
);
182-
})}
183-
</div>
165+
{withNavigation && (
166+
<div className={cnGallery('preview-list')}>
167+
{items.map((item, index) => {
168+
const handleClick = () => {
169+
setActiveItemIndex(index);
170+
};
171+
172+
const selected = activeItemIndex === index;
173+
174+
return (
175+
<button
176+
ref={itemRefs[index]}
177+
type="button"
178+
key={index}
179+
onClick={handleClick}
180+
className={cnGallery('preview-list-item', {selected})}
181+
>
182+
{item.thumbnail}
183+
</button>
184+
);
185+
})}
186+
</div>
187+
)}
184188
</div>
185189
)}
186190
</div>

src/components/Gallery/__stories__/Gallery.stories.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,33 @@ const EmptyGalleryTemplate: StoryFn<GalleryProps> = () => {
183183
};
184184

185185
export const EmptyGallery = EmptyGalleryTemplate.bind({});
186+
187+
const SingleItemGalleryTemplate: StoryFn<GalleryProps> = () => {
188+
const [open, setOpen] = React.useState(false);
189+
190+
const handleToggle = React.useCallback(() => {
191+
setOpen(false);
192+
}, []);
193+
194+
const handleOpen = React.useCallback(() => {
195+
setOpen(true);
196+
}, []);
197+
198+
const imageGalleryItem = getGalleryItemImage({
199+
src: 'https://santreyd.ru/upload/iblock/acc/accd0c751590e792f7e43a05f22472f9.jpg',
200+
name: 'Corgi image',
201+
});
202+
203+
return (
204+
<React.Fragment>
205+
<Button onClick={handleOpen} view="action" size="l">
206+
Open gallery
207+
</Button>
208+
<Gallery open={open} onOpenChange={handleToggle}>
209+
<GalleryItem {...imageGalleryItem} />
210+
</Gallery>
211+
</React.Fragment>
212+
);
213+
};
214+
215+
export const SingleItemGallery = SingleItemGalleryTemplate.bind({});

0 commit comments

Comments
 (0)