|
| 1 | +import { transparentize } from "polished"; |
| 2 | +import * as React from "react"; |
| 3 | +import styled, { createGlobalStyle } from "styled-components"; |
| 4 | +import { s } from "../../styles"; |
| 5 | +import { EditorStyleHelper } from "../styles/EditorStyleHelper"; |
| 6 | + |
| 7 | +type Props = { |
| 8 | + /** An optional caption to display below the image */ |
| 9 | + caption?: string; |
| 10 | + children: React.ReactNode; |
| 11 | +}; |
| 12 | + |
| 13 | +/** |
| 14 | + * Component that wraps an image with the ability to zoom in |
| 15 | + */ |
| 16 | +export const ImageZoom = ({ caption, children }: Props) => { |
| 17 | + const Zoom = React.lazy(() => import("react-medium-image-zoom")); |
| 18 | + const [isActivated, setIsActivated] = React.useState(false); |
| 19 | + |
| 20 | + const handleActivated = React.useCallback(() => { |
| 21 | + setIsActivated(true); |
| 22 | + }, []); |
| 23 | + |
| 24 | + const fallback = ( |
| 25 | + <span onPointerEnter={handleActivated} onFocus={handleActivated}> |
| 26 | + {children} |
| 27 | + </span> |
| 28 | + ); |
| 29 | + |
| 30 | + if (!isActivated) { |
| 31 | + return fallback; |
| 32 | + } |
| 33 | + |
| 34 | + return ( |
| 35 | + <React.Suspense fallback={fallback}> |
| 36 | + <Styles /> |
| 37 | + <Zoom |
| 38 | + zoomMargin={EditorStyleHelper.padding} |
| 39 | + ZoomContent={(props) => <Lightbox caption={caption} {...props} />} |
| 40 | + > |
| 41 | + <div>{children}</div> |
| 42 | + </Zoom> |
| 43 | + </React.Suspense> |
| 44 | + ); |
| 45 | +}; |
| 46 | + |
| 47 | +const Lightbox = ({ |
| 48 | + caption, |
| 49 | + modalState, |
| 50 | + img, |
| 51 | +}: { |
| 52 | + caption: string | undefined; |
| 53 | + modalState: string; |
| 54 | + img: React.ReactNode; |
| 55 | +}) => ( |
| 56 | + <figure> |
| 57 | + {img} |
| 58 | + <Caption $loaded={modalState === "LOADED"}>{caption}</Caption> |
| 59 | + </figure> |
| 60 | +); |
| 61 | + |
| 62 | +const Caption = styled("figcaption")<{ $loaded: boolean }>` |
| 63 | + position: absolute; |
| 64 | + bottom: 0; |
| 65 | + left: 50%; |
| 66 | + transform: translateX(-50%); |
| 67 | + margin-bottom: ${EditorStyleHelper.padding}px; |
| 68 | + font-size: 15px; |
| 69 | + opacity: ${(props) => (props.$loaded ? 1 : 0)}; |
| 70 | + transition: opacity 250ms; |
| 71 | +
|
| 72 | + font-weight: normal; |
| 73 | + color: ${s("textSecondary")}; |
| 74 | +`; |
| 75 | + |
| 76 | +const Styles = createGlobalStyle` |
| 77 | + [data-rmiz] { |
| 78 | + position: relative; |
| 79 | + } |
| 80 | + [data-rmiz-ghost] { |
| 81 | + position: absolute; |
| 82 | + pointer-events: none; |
| 83 | + } |
| 84 | + [data-rmiz-btn-zoom], |
| 85 | + [data-rmiz-btn-unzoom] { |
| 86 | + display: none; |
| 87 | + } |
| 88 | + [data-rmiz-btn-zoom]:not(:focus):not(:active) { |
| 89 | + position: absolute; |
| 90 | + clip: rect(0 0 0 0); |
| 91 | + clip-path: inset(50%); |
| 92 | + height: 1px; |
| 93 | + overflow: hidden; |
| 94 | + pointer-events: none; |
| 95 | + white-space: nowrap; |
| 96 | + width: 1px; |
| 97 | + } |
| 98 | + [data-rmiz-btn-zoom] { |
| 99 | + position: absolute; |
| 100 | + inset: 10px 10px auto auto; |
| 101 | + cursor: zoom-in; |
| 102 | + } |
| 103 | + [data-rmiz-btn-unzoom] { |
| 104 | + position: absolute; |
| 105 | + inset: 20px 20px auto auto; |
| 106 | + cursor: zoom-out; |
| 107 | + z-index: 1; |
| 108 | + } |
| 109 | + [data-rmiz-content="found"] img, |
| 110 | + [data-rmiz-content="found"] svg, |
| 111 | + [data-rmiz-content="found"] [role="img"], |
| 112 | + [data-rmiz-content="found"] [data-zoom] { |
| 113 | + cursor: zoom-in; |
| 114 | + } |
| 115 | + [data-rmiz-modal]::backdrop { |
| 116 | + display: none; |
| 117 | + } |
| 118 | + [data-rmiz-modal][open] { |
| 119 | + position: fixed; |
| 120 | + width: 100vw; |
| 121 | + width: 100dvw; |
| 122 | + height: 100vh; |
| 123 | + height: 100dvh; |
| 124 | + max-width: none; |
| 125 | + max-height: none; |
| 126 | + margin: 0; |
| 127 | + padding: 0; |
| 128 | + border: 0; |
| 129 | + background: transparent; |
| 130 | + overflow: hidden; |
| 131 | + } |
| 132 | + [data-rmiz-modal-overlay] { |
| 133 | + position: absolute; |
| 134 | + inset: 0; |
| 135 | + transition: background-color 0.3s; |
| 136 | + } |
| 137 | + [data-rmiz-modal-overlay="hidden"] { |
| 138 | + background-color: ${(props) => transparentize(1, props.theme.background)}; |
| 139 | + } |
| 140 | + [data-rmiz-modal-overlay="visible"] { |
| 141 | + background-color: ${s("background")}; |
| 142 | + } |
| 143 | + [data-rmiz-modal-content] { |
| 144 | + position: relative; |
| 145 | + width: 100%; |
| 146 | + height: 100%; |
| 147 | + } |
| 148 | + [data-rmiz-modal-img] { |
| 149 | + position: absolute; |
| 150 | + cursor: zoom-out; |
| 151 | + image-rendering: high-quality; |
| 152 | + transform-origin: top left; |
| 153 | + transition: transform 0.3s; |
| 154 | + } |
| 155 | + @media (prefers-reduced-motion: reduce) { |
| 156 | + [data-rmiz-modal-overlay], |
| 157 | + [data-rmiz-modal-img] { |
| 158 | + transition-duration: 0.01ms !important; |
| 159 | + } |
| 160 | + } |
| 161 | +`; |
0 commit comments