<cs-random-content>
ExperimentalHelpersSince 0.1

Selects one or more child elements at random and displays them, hiding the rest.

Randomly picks and displays one or more of its slotted children, hiding the rest. Use it to rotate testimonials, surface featured content, show a tip of the day, or add variety to an otherwise static page.

It looks like you're writing a letter!
Want a hand with the formatting?
It looks like you're building a web app!
I can recommend a few components.
It looks like you're stuck.
Have you tried turning it off and on again?
It looks like you're shipping on a Friday.
Bold move. I respect it.
Shuffle

Examples

Providing Content

Slot virtually any HTML — text, badges, cards, images, or other components — as long as each item is a direct child. Nested elements and bare text nodes are ignored. The host renders display: contents, so it stays invisible to layout.

Plain text works fine.

So do components Even rich cards with their own content.
Shuffle

Number of Items

Set items to show more than one child at a time. The value is clamped to the number of available children.

New Sale Low stock Popular Last chance
1 2 3 4 Shuffle

Changing the Mode

The mode attribute controls how the next selection is chosen. Switch modes and shuffle a few times to feel the difference — the recent picks are listed underneath.

ModeBehaviorBest for
unique defaultNever repeats the previous selection.Tip rotators and timed loops.
randomPicks at complete random, so the same item can appear twice in a row.A one-time shuffle on load.
sequenceSteps through children in DOM order, wrapping at the end (advances by items).Stepping through content in order.
A B C D
unique random sequence Shuffle
Recent picks:

Animating New Content

Use the animation attribute to play an entrance transition when new content is shown.

Good morning!

Welcome back.

What are you building today?

none fade fade-up fade-down fade-left fade-right Next

Directional animations (fade-up, fade-down, fade-left, fade-right) rely on CSS transform, which has no effect on display: inline elements. The component promotes inline children to inline-block while a directional animation plays, so they work inline without extra markup.

Tune the duration, easing, and travel distance with the --animation-duration, --animation-easing, and --animation-translate custom properties. Animations are skipped automatically when the user prefers reduced motion.

Autoplay

Add the autoplay attribute to rotate content on a timer, and set the cadence with autoplay-interval (milliseconds). It pauses while the pointer is over the component or focus is inside it, and resumes when the user moves away. It respects reduced motion, too: content still rotates, but the entrance animation is skipped. Each new item is announced to screen readers using its text, so give icon-only content an accessible label (for example <cs-icon label="…">).

Did you know?
Octopuses have three hearts.
Honey never spoils.
A group of flamingos is called a flamboyance.
Bananas are botanically berries.
Cheetahs meow rather than roar.
Pause

If you turn on autoplay, give people a way to pause it.
The built-in hover and focus pausing doesn’t help someone using a keyboard when the rotating content isn’t focusable, so add a visible pause button like the one above.

Styling the Container

The host is display: contents by default, so it adds no box of its own. To lay several shown items out as a row or grid, give the host its own display — that overrides the transparent default. Here it shows three of six people at random in a flex row.

Shuffle

Because the host is transparent, the component also works inline within a sentence.

Have a wonderful fantastic marvelous splendid day!

Shuffle

Unselected children are hidden with the hidden attribute, so you can target whatever is currently shown with :not([hidden]):

cs-random-content > :not([hidden]) {
  outline: var(--cs-border-width-s) solid var(--cs-color-brand-fill-loud);
}

Reacting to Changes

The component emits a cs-content-change event whenever the displayed selection changes — on first render, on randomize(), and on each autoplay tick. event.detail.items is the array of elements now shown.

const rc = document.querySelector('cs-random-content');

rc.addEventListener('cs-content-change', event => {
  console.log('Now showing:', event.detail.items);
});

Server-Side Rendering

Until the component upgrades on the client, every child is visible, which can flash on first paint. Add the cs-cloak class to hide content until Cornerstone is ready. Because the first selection is random, server- and client-rendered output can also differ; for a stable first paint, use mode="sequence", which always starts at the first child.

Using a Framework

The component selects from its slotted children by toggling the hidden attribute on them directly. Treat that content as static — if a framework owns and re-renders the children, its reconciliation can overwrite the hidden state. Render a fixed set of children, then drive the component imperatively: call randomize() through a ref and listen for cs-content-change.

API

Importing

If you're using the autoloader or a hosted project, components load on demand — no manual import needed. To cherry-pick this component, use one of the following snippets.

npm Self-Hosted React
import '@cruglobal/cornerstone-components/components/random-content/random-content.js';
import './cornerstone/components/random-content/random-content.js';
import CsRandomContent from '@cruglobal/cornerstone-components/react/random-content/index.js';

Slots

Learn more about using slots.

NameDescription
(default)The pool of children to choose from. Only direct element children are eligible; unselected children are hidden with the hidden attribute.

Attributes & Properties

Learn more about attributes and properties.

PropertyAttributeDescriptionTypeDefaultReflects
animationanimationEntrance animation for newly shown children.'none' | 'fade' | 'fade-up' | 'fade-down' | 'fade-left' | 'fade-right''none'Yes
autoplayautoplayRotate the content automatically. Set the cadence with autoplay-interval.booleanfalseYes
autoplayIntervalautoplay-intervalAutoplay cadence in milliseconds.number3000
itemsitemsNumber of children to show simultaneously. Clamped to [1, childCount].number1
modemodeSelection strategy: unique (default), random, or sequence.'random' | 'unique' | 'sequence''unique'Yes

Methods

Learn more about methods.

NameDescriptionArguments
randomize()Selects a new set of children using the current mode. Returns the elements now shown.

Events

Learn more about events.

NameDescription
cs-content-changeEmitted whenever the displayed selection changes, including on first render, on randomize(), and on each autoplay tick.

CSS Custom Properties

Learn more about CSS custom properties.

NameDescriptionDefault
--animation-durationDuration of the entrance animation. Default is 300ms.
--animation-easingEasing function for the entrance animation. Default is ease.
--animation-translateTranslation distance for directional animations (fade-up, fade-down, fade-left, fade-right). Default is 0.5em.