Skip to content

Instantly share code, notes, and snippets.

@pavanpej
Last active July 26, 2024 02:32
Show Gist options
  • Save pavanpej/857835d2b59d09ed0162b2bd59f80cb7 to your computer and use it in GitHub Desktop.
Save pavanpej/857835d2b59d09ed0162b2bd59f80cb7 to your computer and use it in GitHub Desktop.
"Yet Another React Lightbox" custom plugin for image rotation
// based off of https://stackblitz.com/edit/yet-another-react-lightbox-examples?file=src/examples/FullscreenPlugin.tsx&initialPath=/examples/basic
import * as React from "react";
import Lightbox from "yet-another-react-lightbox";
import Rotate from "@/examples/RotatePlugin"
import { LightboxButton, Paragraph, Title } from "@/components";
import slides from "@/data/slides";
export default function RotateExample() {
const [open, setOpen] = React.useState(false);
return (
<>
<Title>Rotate Plugin</Title>
<Paragraph>
Rotate plugin adds the "rotate" button.
</Paragraph>
<LightboxButton
onClick={() => {
setOpen(true);
}}
/>
<LightboxButton
onClick={() => {
setOpen(true);
}}
/>
<Lightbox
open={open}
close={() => setOpen(false)}
slides={slides}
plugins={[Rotate]}
/>
</>
);
}
import * as React from "react";
import { useState } from "react";
import {
addToolbarButton,
PluginProps,
Slide,
} from "yet-another-react-lightbox";
export default function Rotate({ augment }: PluginProps) {
const [rotationAngle, setRotationAngle] = useState(0);
const rightRotateButton = (
<button
key="to-right-btn"
type="button"
onClick={() => {
setRotationAngle((prevAngle) => prevAngle + 90);
}}
>
R
</button>
);
const renderSlide = ({ slide }: { slide: Slide }): React.ReactNode => {
// Custom rendering logic for slide
// Return a React node to render the custom slide
return (
<img
src={slide.src}
alt={slide.alt}
style={{
transform: `rotate(${rotationAngle}deg)`,
transition: "transform 0.4s ease-in-out 0s",
}}
/>
);
};
augment(({ toolbar, download, render, ...restProps }) => ({
toolbar: addToolbarButton(toolbar, "Rotate", rightRotateButton),
render: {
slide: renderSlide,
},
...restProps,
}));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment