|
| 1 | +import React, { useState, useRef, useEffect } from 'react' |
| 2 | +import { motion, useElementScroll } from 'framer-motion' |
| 3 | +import clsx from 'clsx' |
| 4 | + |
| 5 | +const viewport = { |
| 6 | + lvh: 491, |
| 7 | + svh: 443, |
| 8 | + navBarHeight: 48, |
| 9 | + width: 300, |
| 10 | + padding: 5, |
| 11 | +} |
| 12 | + |
| 13 | +const transition = { |
| 14 | + duration: 0.2, |
| 15 | + ease: 'linear', |
| 16 | + type: 'tween', |
| 17 | +} |
| 18 | +export function DynamicViewportExample({ |
| 19 | + unit = 'dvh', |
| 20 | + colorStyles = 'dark:bg-blue-500 bg-blue-500 border border-blue-400', |
| 21 | + containerBackground = 'bg-stripes-blue', |
| 22 | +}) { |
| 23 | + const [hidden, setHidden] = useState(false) |
| 24 | + const parent = useRef(null) |
| 25 | + |
| 26 | + const { scrollY } = useElementScroll(parent) |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + const unsub = scrollY.onChange((latest) => { |
| 30 | + const previous = scrollY.getPrevious() |
| 31 | + if (latest > previous) { |
| 32 | + setHidden(true) |
| 33 | + } else { |
| 34 | + setHidden(false) |
| 35 | + } |
| 36 | + }) |
| 37 | + |
| 38 | + return () => unsub() |
| 39 | + }) |
| 40 | + |
| 41 | + // NOTE:When upgrading Framer Motion we can use useMotionValueEvent instead: |
| 42 | + |
| 43 | + // import { motion, useScroll, useMotionValueEvent } from 'framer-motion' |
| 44 | + |
| 45 | + // const { scrollY } = useScroll({ container: parent }) |
| 46 | + |
| 47 | + // useMotionValueEvent(scrollY, 'change', (latest) => { |
| 48 | + // const previous = scrollY.getPrevious() |
| 49 | + // if (latest > previous) { |
| 50 | + // setHidden(true) |
| 51 | + // } else { |
| 52 | + // setHidden(false) |
| 53 | + // } |
| 54 | + // }) |
| 55 | + |
| 56 | + return ( |
| 57 | + <> |
| 58 | + <div className="not-prose mb-4"> |
| 59 | + <div className="flex space-x-2"> |
| 60 | + <svg |
| 61 | + className="flex-none w-5 h-5" |
| 62 | + viewBox="0 0 20 20" |
| 63 | + fill="none" |
| 64 | + strokeWidth="1.5" |
| 65 | + strokeLinecap="round" |
| 66 | + strokeLinejoin="round" |
| 67 | + aria-hidden="true" |
| 68 | + > |
| 69 | + <path |
| 70 | + d="m9.813 9.25.346-5.138a1.276 1.276 0 0 0-2.54-.235L6.75 11.25 5.147 9.327a1.605 1.605 0 0 0-2.388-.085.018.018 0 0 0-.004.019l1.98 4.87a5 5 0 0 0 4.631 3.119h3.885a4 4 0 0 0 4-4v-1a3 3 0 0 0-3-3H9.813Z" |
| 71 | + className="stroke-slate-400 dark:stroke-slate-300" |
| 72 | + /> |
| 73 | + <path |
| 74 | + d="M3 5s.35-.47 1.25-.828m9.516-.422c2.078.593 3.484 1.5 3.484 1.5" |
| 75 | + className="stroke-slate-400 dark:stroke-sky-400" |
| 76 | + /> |
| 77 | + </svg> |
| 78 | + <p className="text-slate-700 text-sm font-medium dark:text-slate-200"> |
| 79 | + Scroll up and down in the viewport to hide/show the browser UI |
| 80 | + </p> |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + <div className="not-prose relative bg-slate-50 rounded-xl overflow-hidden dark:bg-slate-800/25 md:px-8 py-8 px-4"> |
| 84 | + <div className="relative grid justify-items-center"> |
| 85 | + <div |
| 86 | + ref={parent} |
| 87 | + style={{ width: `${viewport.width}px`, height: `${viewport.lvh}px` }} |
| 88 | + className={clsx( |
| 89 | + containerBackground, |
| 90 | + 'relative rounded-lg border dark:border-slate-700 overscroll-none border-slate-300 text-center text-xs overflow-y-scroll no-scrollbar' |
| 91 | + )} |
| 92 | + > |
| 93 | + <motion.div |
| 94 | + className="absolute w-full overflow-hidden snap-start" |
| 95 | + transition={{ |
| 96 | + ...transition, |
| 97 | + }} |
| 98 | + variants={{ |
| 99 | + visible: { |
| 100 | + y: 0, |
| 101 | + height: `${viewport.lvh}px`, |
| 102 | + }, |
| 103 | + hidden: { |
| 104 | + y: `-${viewport.navBarHeight - 2}px`, |
| 105 | + height: `${viewport.lvh + viewport.navBarHeight - 1}px`, |
| 106 | + }, |
| 107 | + }} |
| 108 | + initial="visible" |
| 109 | + animate={hidden ? 'hidden' : 'visible'} |
| 110 | + > |
| 111 | + <div className=" w-full grid grid-cols-[auto,1fr,auto] items-center justify-start gap-4 rounded-t-lg border-b border-slate-300 dark:border-slate-600 dark:bg-slate-800 bg-slate-100 px-3 h-[48px]"> |
| 112 | + <svg |
| 113 | + xmlns="http://www.w3.org/2000/svg" |
| 114 | + fill="none" |
| 115 | + viewBox="0 0 24 24" |
| 116 | + strokeWidth="1.5" |
| 117 | + stroke="currentColor" |
| 118 | + className="h-5 w-5 text-slate-600 dark:text-slate-400" |
| 119 | + > |
| 120 | + <path |
| 121 | + strokeLinecap="round" |
| 122 | + strokeLinejoin="round" |
| 123 | + d="M2.25 12l8.954-8.955c.44-.439 1.152-.439 1.591 0L21.75 12M4.5 9.75v10.125c0 .621.504 1.125 1.125 1.125H9.75v-4.875c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125V21h4.125c.621 0 1.125-.504 1.125-1.125V9.75M8.25 21h8.25" |
| 124 | + /> |
| 125 | + </svg> |
| 126 | + <div className="w-full rounded-full border border-slate-200 dark:border-slate-700 bg-slate-50 dark:bg-slate-700 px-4 py-1 text-slate-600 dark:text-slate-400"> |
| 127 | + tailwindcss.com |
| 128 | + </div> |
| 129 | + <svg |
| 130 | + xmlns="http://www.w3.org/2000/svg" |
| 131 | + fill="none" |
| 132 | + viewBox="0 0 24 24" |
| 133 | + strokeWidth="1.5" |
| 134 | + stroke="currentColor" |
| 135 | + className="h-5 w-5 text-slate-600 dark:text-slate-400" |
| 136 | + > |
| 137 | + <path |
| 138 | + strokeLinecap="round" |
| 139 | + strokeLinejoin="round" |
| 140 | + d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0l3.181 3.183a8.25 8.25 0 0013.803-3.7M4.031 9.865a8.25 8.25 0 0113.803-3.7l3.181 3.182m0-4.991v4.99" |
| 141 | + /> |
| 142 | + </svg> |
| 143 | + </div> |
| 144 | + <motion.div |
| 145 | + className="w-full p-[7px] h-full pointer-events-none " |
| 146 | + transition={{ |
| 147 | + ...transition, |
| 148 | + duration: 0.2, |
| 149 | + // duration: unit === 'dvh' ? 0.4 : 0.2, |
| 150 | + delay: unit === 'dvh' ? 0.4 : 0, |
| 151 | + }} |
| 152 | + variants={{ |
| 153 | + visible: { |
| 154 | + maxHeight: `${unit === 'lvh' ? viewport.lvh : viewport.svh - 2}px`, |
| 155 | + }, |
| 156 | + hidden: { |
| 157 | + maxHeight: `${unit === 'svh' ? viewport.svh : viewport.lvh - 1}px`, |
| 158 | + }, |
| 159 | + }} |
| 160 | + initial="visible" |
| 161 | + animate={hidden ? 'hidden' : 'visible'} |
| 162 | + > |
| 163 | + <div |
| 164 | + className={clsx( |
| 165 | + colorStyles, |
| 166 | + 'text-slate-50 font-mono font-bold py-4 w-full h-full gap-5 rounded-md grid grid-rows-[1fr_auto_1fr] items-center content-center self-center justify-items-center overflow-hidden' |
| 167 | + )} |
| 168 | + > |
| 169 | + {/* <div className="grid grid-rows-[1fr] h-full items-start overflow-y-hidden"> |
| 170 | + <svg |
| 171 | + className="w-[12px] text-pink-300" |
| 172 | + viewBox="0 0 9 162" |
| 173 | + fill="none" |
| 174 | + xmlns="http://www.w3.org/2000/svg" |
| 175 | + > |
| 176 | + <path |
| 177 | + d="M4.5 161.5V1M4.5 1L8.5 5M4.5 1L0.5 5" |
| 178 | + stroke="currentColor" |
| 179 | + // strokeWidth={1.5} |
| 180 | + stroke-linecap="round" |
| 181 | + stroke-linejoin="round" |
| 182 | + /> |
| 183 | + </svg> |
| 184 | + </div> */} |
| 185 | + <div className="grid grid-rows-[1px_1fr] h-full justify-items-center"> |
| 186 | + <div className="bg-white/60 w-[12px] h-full"></div> |
| 187 | + <div className="bg-white/40 w-[1.5px] h-full"></div> |
| 188 | + </div> |
| 189 | + <p>h-{unit}</p> |
| 190 | + |
| 191 | + {/* <div className="grid grid-rows-[1fr] h-full items-end overflow-y-hidden"> |
| 192 | + <svg |
| 193 | + className="w-[12px] text-pink-300" |
| 194 | + viewBox="0 0 10 162" |
| 195 | + fill="none" |
| 196 | + xmlns="http://www.w3.org/2000/svg" |
| 197 | + > |
| 198 | + <path |
| 199 | + d="M5.00001 1L5 161.5M5 161.5L1 157.5M5 161.5L9 157.5" |
| 200 | + stroke="currentColor" |
| 201 | + // strokeWidth={1.5} |
| 202 | + stroke-linecap="round" |
| 203 | + stroke-linejoin="round" |
| 204 | + /> |
| 205 | + </svg> |
| 206 | + </div> */} |
| 207 | + <div className="grid grid-rows-[1fr_1px] h-full justify-items-center"> |
| 208 | + <div className="bg-white/40 w-[1.5px] h-full"></div> |
| 209 | + <div className="bg-white/60 w-[12px] h-full"></div> |
| 210 | + </div> |
| 211 | + </div> |
| 212 | + </motion.div> |
| 213 | + </motion.div> |
| 214 | + </div> |
| 215 | + {unit === 'lvh' && ( |
| 216 | + <motion.div |
| 217 | + transition={{ |
| 218 | + ...transition, |
| 219 | + }} |
| 220 | + variants={{ |
| 221 | + visible: { |
| 222 | + bottom: `-${viewport.navBarHeight}px`, |
| 223 | + height: `${viewport.navBarHeight}px`, |
| 224 | + }, |
| 225 | + hidden: { |
| 226 | + bottom: '5px', |
| 227 | + height: '0px', |
| 228 | + }, |
| 229 | + }} |
| 230 | + initial="visible" |
| 231 | + animate={hidden ? 'hidden' : 'visible'} |
| 232 | + // For some reason putting this in a tailwind class doesn't work |
| 233 | + style={{ width: `${viewport.width - 12}px` }} |
| 234 | + className={clsx( |
| 235 | + colorStyles, |
| 236 | + 'opacity-20 rounded-b-md absolute left-0 right-0 mx-auto' |
| 237 | + )} |
| 238 | + ></motion.div> |
| 239 | + )}{' '} |
| 240 | + </div> |
| 241 | + <div |
| 242 | + style={{ backgroundPosition: '10px 10px' }} |
| 243 | + className=" z-[-1] pointer-events-none absolute inset-0 bg-grid-slate-100 [mask-image:linear-gradient(0deg,#fff,rgba(255,255,255,0.6))] dark:bg-grid-slate-700/25 dark:[mask-image:linear-gradient(0deg,rgba(255,255,255,0.1),rgba(255,255,255,0.5))]" |
| 244 | + /> |
| 245 | + <div className="z-[-1] pointer-events-none absolute inset-0 border border-black/5 rounded-xl dark:border-white/5" /> |
| 246 | + </div> |
| 247 | + </> |
| 248 | + ) |
| 249 | +} |
0 commit comments