diff --git a/.changeset/every-worms-crash.md b/.changeset/every-worms-crash.md new file mode 100644 index 00000000..5fb5d6b7 --- /dev/null +++ b/.changeset/every-worms-crash.md @@ -0,0 +1,5 @@ +--- +"@wethegit/react-marquee": minor +--- + +Adds an optional prop to enable vertical marquee functionality. diff --git a/README.md b/README.md index b04fc52e..5c09bbad 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,8 @@ const YourComponent = () => { | reducedMotionSpeed | Number | 20 | A customisable value used to set the speed of the component if `prefersReducedMotion` is true. | | prefersReducedMotion | Boolean | false | Whether the user prefers reduced motion. If `true`, the component will default to a much slower speed, unless a custom speed is specified using: `reducedMotionSpeed`. | | playing | Boolean | true | Whether the marquee animation is playing or paused. | -| reverse | Boolean | false | Whether the marquee should animate from left to right. | +| reverse | Boolean | false | Whether the marquee should animate from left to right (or top to bottom, if `vertical` is passed as a prop). | +| vertical | Boolean | false | Whether the marquee should animate from bottom to top. __IMPORTANT__: for vertical marquees to work, the direct parent element of the marquee *must* have a defined height (100dvh, etc) and use `display: flex` (and, likely, `overflow: clip`) | className | String | | Optional class added to the parent element. | | children | Node | | The child elements rendered within the component. In the example above these are images, however they can be any type of element. | diff --git a/src/lib/marquee.module.css b/src/lib/marquee.module.css index c4217321..3524d1de 100644 --- a/src/lib/marquee.module.css +++ b/src/lib/marquee.module.css @@ -8,6 +8,16 @@ } } +@keyframes scroll-vertical { + 0% { + transform: translateY(0%); + } + + 100% { + transform: translateY(calc(var(--marquee-width) * -1px)); + } +} + .Marquee { position: relative; white-space: nowrap; @@ -33,6 +43,22 @@ * loading up the animation. */ animation: scroll var(--duration) linear infinite var(--animation-state, running); + + &.Marquee--vertical { + animation: scroll-vertical var(--duration) linear infinite + var(--animation-state, running); + + &.Marquee--reverse { + animation-direction: reverse; + } + } +} + +.Marquee--vertical { + .Marquee__Slide { + display: flex; + flex-direction: column; + } } .Marquee--reverse { diff --git a/src/lib/marquee.tsx b/src/lib/marquee.tsx index 3f913844..1f4b4c5e 100644 --- a/src/lib/marquee.tsx +++ b/src/lib/marquee.tsx @@ -28,9 +28,13 @@ export interface MarqueeProps extends React.ComponentPropsWithoutRef<"div"> { */ speed?: number /** - * Whether to animate from left to right + * Whether to animate from left to right, or bottom to top */ reverse?: boolean + /** + * Whether to animate vertically, from top to bottom. Direct parent node should have display: flex & a defined height (100dvh, etc) + */ + vertical?: boolean } type Timer = ReturnType @@ -41,11 +45,12 @@ export function Marquee({ prefersReducedMotion = false, playing = true, reverse, + vertical, children, className, ...props }: MarqueeProps) { - const [marqueeWidth, setMarqueeWidth] = useState(0) + const [marqueeSize, setmarqueeSize] = useState(0) const [duration, setDuration] = useState(0) const [neededAmount, setNeededAmount] = useState(1) @@ -54,15 +59,35 @@ export function Marquee({ const resizeTimer = useRef() const updateState = useCallback(() => { - const containerWidth = container.current?.clientWidth || 0 - const marqueeWidth = marquee.current?.clientWidth || 0 + const getRelevantDimensions = () => { + if (vertical) { + return { + containerSize: container.current?.clientHeight || 0, + marqueeSize: marquee.current?.clientHeight || 0, + } + } + + return { + containerSize: container.current?.clientWidth || 0, + marqueeSize: marquee.current?.clientWidth || 0, + } + } + + const { containerSize, marqueeSize } = getRelevantDimensions() // If prefersReducedMotion, replace speed with reducedMotionSpeed const speedAmount = prefersReducedMotion ? reducedMotionSpeed : speed // Divide the container width, by the marquee width and round it up // to give us the ammount of items needed to fill the container. - let neededAmount = Math.ceil(containerWidth / marqueeWidth) * 2 - 1 + const viewportSize = vertical ? window.innerHeight : window.innerWidth + let neededAmount = + Math.min( + Math.ceil(containerSize / marqueeSize), + Math.ceil(viewportSize / marqueeSize) + ) * + 2 - + 1 if (neededAmount < 1 || isNaN(neededAmount) || neededAmount === Infinity) { neededAmount = 1 @@ -73,15 +98,15 @@ export function Marquee({ // Set marquee width in state so we can use it for the`--marquee-width` // inline style. - setMarqueeWidth(marqueeWidth) + setmarqueeSize(marqueeSize) // Set duration speed, which is used below in the --duration inline style. - if (marqueeWidth < containerWidth) { - setDuration(containerWidth / speedAmount) + if (marqueeSize < containerSize) { + setDuration(containerSize / speedAmount) } else { - setDuration(marqueeWidth / speedAmount) + setDuration(marqueeSize / speedAmount) } - }, [prefersReducedMotion, reducedMotionSpeed, speed]) + }, [prefersReducedMotion, reducedMotionSpeed, speed, vertical]) useEffect(() => { const el = container.current @@ -111,13 +136,14 @@ export function Marquee({ ref={container} className={classnames([ styles.Marquee, - marqueeWidth > 0 && styles["Marquee--ready"], + vertical && styles["Marquee--vertical"], + marqueeSize > 0 && styles["Marquee--ready"], reverse && styles["Marquee--reverse"], className, ])} style={ { - "--marquee-width": marqueeWidth, + "--marquee-width": marqueeSize, "--duration": duration + `s`, "--animation-state": playing ? "running" : "paused", } as React.CSSProperties diff --git a/src/main.tsx b/src/main.tsx index c3c3c650..6fe1d8ae 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -20,6 +20,20 @@ function App() { + +

Vertical

+
+ + + +
+ +

Vertical Reverse

+
+ + + +
) }