Skip to content

Commit

Permalink
feat: add config option to disable zoom (fix #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
igordanchenko committed Nov 23, 2024
1 parent c5e3a62 commit 208f1f6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,24 @@ Type: `string`
CSS class of the lightbox root element. You can use this class name to provide
module-scoped style overrides.

### zoom

Type: `object`

Zoom settings.

- `disabled` - disable zoom on image slides
- `supports` - zoom-enabled custom slide types

Usage example:

```tsx
<Lightbox
zoom={{ supports: ["custom-slide-type"] }}
// ...
/>
```

## Custom Slide Attributes

You can add custom slide attributes with the following module augmentation.
Expand Down
4 changes: 2 additions & 2 deletions src/components/Zoom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default function Zoom({ children }: PropsWithChildren) {
const observer = useRef<ResizeObserver>();
const carouselRef = useRef<HTMLDivElement | null>(null);

const { index, slides, zoom: { supports } = {} } = useLightboxContext();
const { index, slides, zoom: { supports, disabled } = {} } = useLightboxContext();

const [prevIndex, setPrevIndex] = useState(index);
if (index !== prevIndex) {
Expand All @@ -72,7 +72,7 @@ export default function Zoom({ children }: PropsWithChildren) {
}

const slide = slides[index];
const maxZoom = isImageSlide(slide) || (supports || []).includes((slide as any).type) ? 8 : 1;
const maxZoom = (isImageSlide(slide) && !disabled) || (supports || []).includes((slide as any).type) ? 8 : 1;

useLayoutEffect(() => {
const carouselHalfWidth = (rect?.width || 0) / 2;
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ export interface ControllerSettings {

/** Zoom settings */
export interface ZoomSettings {
/** disable zoom on image slides */
disabled?: boolean;
/** zoom-enabled custom slide types */
supports?: SlideTypeKey[];
}
Expand Down
9 changes: 9 additions & 0 deletions test/Lightbox.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,15 @@ describe("Lightbox", () => {
expectToBeZoomedOut();
});

it("supports disabling zoom", async () => {
const user = userEvent.setup();

renderLightbox({ zoom: { disabled: true } });

await user.dblClick(getController());
expectToBeZoomedOut();
});

it("supports zoom on custom slides", () => {
let maxZoom: number | undefined;

Expand Down

0 comments on commit 208f1f6

Please sign in to comment.