-
Notifications
You must be signed in to change notification settings - Fork 21
feat(Gallery): add built-in image rotation support #378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
3455e23
59c1144
96cb287
c68e7a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, moved GalleryContext before modal comp. Now should be fine |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import * as React from 'react'; | ||
|
|
||
| export type GalleryImageRotationContextValue = { | ||
| rotation: number; | ||
| rotateLeft: () => void; | ||
| rotateRight: () => void; | ||
| }; | ||
|
|
||
| const GalleryImageRotationContext = React.createContext<GalleryImageRotationContextValue>({ | ||
| rotation: 0, | ||
| rotateLeft: () => {}, | ||
| rotateRight: () => {}, | ||
| }); | ||
|
|
||
| export type GalleryImageRotationProviderProps = | ||
| React.PropsWithChildren<GalleryImageRotationContextValue>; | ||
|
|
||
| export const GalleryImageRotationProvider: React.FunctionComponent<GalleryImageRotationProviderProps> = | ||
| function GalleryImageRotationProvider({children, rotation, rotateLeft, rotateRight}) { | ||
| const value = React.useMemo( | ||
| () => ({rotation, rotateLeft, rotateRight}), | ||
| [rotation, rotateLeft, rotateRight], | ||
| ); | ||
| return ( | ||
| <GalleryImageRotationContext.Provider value={value}> | ||
| {children} | ||
| </GalleryImageRotationContext.Provider> | ||
| ); | ||
| }; | ||
|
|
||
| export const useGalleryImageRotationContext = () => React.useContext(GalleryImageRotationContext); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './GalleryImageRotationContext'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export const ROTATION_STEP = 90; // degrees per rotate-left / rotate-right action | ||
| export const FULL_ROTATION = 360; // degrees in a full rotation |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './useImageRotation'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import * as React from 'react'; | ||
|
|
||
| import {useGalleryImageRotationContext} from '../../contexts/GalleryImageRotationContext'; | ||
|
|
||
| import {FULL_ROTATION, ROTATION_STEP} from './constants'; | ||
|
|
||
| export type UseImageRotationReturn = { | ||
| /** | ||
| * Styles for the `<img>` element: the `rotate` transform part plus | ||
| * swapped max-width/max-height constraints for 90°/270° rotations. | ||
| */ | ||
| imageRotationStyles: React.CSSProperties; | ||
| rotation: number; | ||
| rotateLeft: () => void; | ||
| rotateRight: () => void; | ||
| setContainerDims: React.Dispatch<React.SetStateAction<{width: number; height: number}>>; | ||
| }; | ||
|
|
||
| /** Hook for reading image rotation state from GalleryImageRotationContext. */ | ||
| export function useImageRotation(): UseImageRotationReturn { | ||
| const {rotation, rotateLeft, rotateRight} = useGalleryImageRotationContext(); | ||
| const [containerDims, setContainerDims] = React.useState({width: 0, height: 0}); | ||
|
|
||
| const normalizedRotation = ((rotation % FULL_ROTATION) + FULL_ROTATION) % FULL_ROTATION; | ||
| const isHorizontalRotation = | ||
| normalizedRotation === ROTATION_STEP || | ||
| normalizedRotation === FULL_ROTATION - ROTATION_STEP; | ||
|
|
||
| const imageRotationStyles = React.useMemo<React.CSSProperties>( | ||
| () => ({ | ||
| ...(rotation ? {transform: `rotate(${rotation}deg)`} : {}), | ||
| ...(isHorizontalRotation && containerDims.width > 0 | ||
| ? {maxWidth: containerDims.height, maxHeight: containerDims.width} | ||
| : {}), | ||
| }), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should rotation occur if the image dimensions haven't been set yet? I don't think so. The |
||
| [rotation, isHorizontalRotation, containerDims], | ||
| ); | ||
|
|
||
| return {imageRotationStyles, rotation, rotateLeft, rotateRight, setContainerDims}; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be moved to a separate hook –
useRotationState(), to which you can pass the initial value and custom rotation step as arguments.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but i think for now it enough to keep this hook in Gallery/Image scope without args