Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/every-worms-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wethegit/react-marquee": minor
---

Adds an optional prop to enable vertical marquee functionality.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |

Expand Down
26 changes: 26 additions & 0 deletions src/lib/marquee.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
50 changes: 38 additions & 12 deletions src/lib/marquee.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof setTimeout>
Expand All @@ -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)

Expand All @@ -54,15 +59,35 @@ export function Marquee({
const resizeTimer = useRef<Timer>()

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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ function App() {
<Marquee playing={false}>
<Slide />
</Marquee>

<h1>Vertical</h1>
<div style={{ display: "flex", height: "100vh", overflow: "clip" }}>
<Marquee vertical speed={100} reducedMotionSpeed={20}>
<Slide />
</Marquee>
</div>

<h1>Vertical Reverse</h1>
<div style={{ display: "flex", height: "100vh", overflow: "clip" }}>
<Marquee vertical speed={100} reducedMotionSpeed={20} reverse>
<Slide />
</Marquee>
</div>
</>
)
}
Expand Down
Loading