Skip to content
Open
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ export interface UncontrolledProps {
// be from the window's boundaries.
// Default: 0
zoomMargin?: number

// Align the zoomed image vertically to the top or center of the viewport.
// Default: 'center'
zoomAlignmentY?: 'top' | 'center' | 'bottom'
}
```

Expand Down
5 changes: 5 additions & 0 deletions source/Controlled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export interface ControlledProps {
onUnzoom: () => void
}) => React.ReactElement
zoomImg?: React.ImgHTMLAttributes<HTMLImageElement>
zoomAlignmentY?: 'top' | 'center' | 'bottom'
zoomMargin?: number
}

Expand All @@ -87,6 +88,7 @@ interface ControlledDefaultProps {
IconZoom: React.ElementType
swipeToUnzoomThreshold: number
wrapElement: 'div' | 'span'
zoomAlignmentY: 'top' | 'center' | 'bottom'
zoomMargin: number
}

Expand All @@ -110,6 +112,7 @@ class ControlledBase extends React.Component<ControlledPropsWithDefaults, Contro
IconZoom: IEnlarge,
swipeToUnzoomThreshold: 10,
wrapElement: 'div',
zoomAlignmentY: 'center',
zoomMargin: 0,
}

Expand Down Expand Up @@ -160,6 +163,7 @@ class ControlledBase extends React.Component<ControlledPropsWithDefaults, Contro
wrapElement: WrapElement,
ZoomContent,
zoomImg,
zoomAlignmentY,
zoomMargin,
},
refContent,
Expand Down Expand Up @@ -227,6 +231,7 @@ class ControlledBase extends React.Component<ControlledPropsWithDefaults, Contro
offset: zoomMargin,
shouldRefresh,
targetEl: imgEl as SupportedImage,
zoomAlignmentY,
})
: {}

Expand Down
17 changes: 16 additions & 1 deletion source/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ export interface GetStyleModalImg {
offset: number,
shouldRefresh: boolean,
targetEl: SupportedImage,
zoomAlignmentY: 'top' | 'center' | 'bottom'
}): React.CSSProperties
}

Expand All @@ -450,6 +451,7 @@ export const getStyleModalImg: GetStyleModalImg = ({
offset,
shouldRefresh,
targetEl,
zoomAlignmentY,
}) => {
const hasScalableSrc =
isSvg ||
Expand Down Expand Up @@ -519,7 +521,20 @@ export const getStyleModalImg: GetStyleModalImg = ({
const childCenterY = parseFloat(String(style.top || 0)) + (parseFloat(String(style.height || 0)) / 2)

const translateX = viewportX - childCenterX
const translateY = viewportY - childCenterY
let translateY

switch (zoomAlignmentY) {
case 'top':
translateY = -style.top;
break
case 'bottom':
translateY = window.innerHeight - style.height - style.top;
break
case 'center':
default:
translateY = viewportY - childCenterY
break
}

// For scenarios like resizing the browser window
if (shouldRefresh) {
Expand Down