Random Content
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.
Want a hand with the formatting?
I can recommend a few components.
Have you tried turning it off and on again?
Bold move. I respect it.
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.
Setting the Number of Items
Set items to show more than one child at a time. The value is clamped to the number of available children.
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.
| Mode | Behavior | Best for |
|---|---|---|
unique |
Never repeats the previous selection. | Tip rotators and timed loops. |
random |
Picks at complete random, so the same item can appear twice in a row. | A one-time shuffle on load. |
sequence |
Steps through children in DOM order, wrapping at the end (advances by items). |
Stepping through content in order. |
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?
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 <wa-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.
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.
Because the host is transparent, the component also works inline within a sentence.
Have a
Unselected children are hidden with the hidden attribute, so you can target whatever is currently shown with :not([hidden]):
wa-random-content > :not([hidden]) { outline: var(--wa-border-width-s) solid var(--wa-color-brand-fill-loud); }
Reacting to Changes
The component emits a wa-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('wa-random-content'); rc.addEventListener('wa-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 wa-cloak class to hide content until Web Awesome 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 wa-content-change.
Importing
If you're using the autoloader or a hosted project, components load on demand — no manual import needed. To cherry-pick a component manually, use one of the following snippets.
Import this component directly from the CDN:
import 'https://ka-f.webawesome.com/webawesome@0.17.0/components/random-content/random-content.js';
After installing Web Awesome via npm, import this component:
import '@awesome.me/webawesome/dist/components/random-content/random-content.js';
If you're self-hosting Web Awesome, import this component from your server:
import './webawesome/dist/components/random-content/random-content.js';
To import this component for React 18 or below, use the following code:
import WaRandomContent from '@awesome.me/webawesome/dist/react/random-content/index.js';
Slots
Learn more about using slots.
| Name | Description |
|---|---|
| (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.
| Name | Description | Reflects |
|---|---|---|
animationanimation |
Entrance animation for newly shown children.
Type
'none' | 'fade' | 'fade-up' | 'fade-down' | 'fade-left' | 'fade-right'
Default
'none'
|
|
autoplayautoplay |
Rotate the content automatically. Set the cadence with
autoplay-interval.
Type
boolean
Default
false
|
|
autoplayIntervalautoplay-interval |
Autoplay cadence in milliseconds.
Type
number
Default
3000
|
|
itemsitems |
Number of children to show simultaneously. Clamped to [1, childCount].
Type
number
Default
1
|
|
modemode |
Selection strategy:
unique (default), random, or sequence.
Type
'random' | 'unique' | 'sequence'
Default
'unique'
|
|
Methods
Learn more about methods.
| Name | Description | Arguments |
|---|---|---|
randomize() |
Selects a new set of children using the current mode. Returns the elements now shown. |
Events
Learn more about events.
| Name | Description |
|---|---|
wa-content-change |
Emitted 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.
| Name | Description |
|---|---|
--animation-duration |
Duration of the entrance animation. Default is
300ms. |
--animation-easing |
Easing function for the entrance animation. Default is
ease. |
--animation-translate |
Translation distance for directional animations (
fade-up, fade-down, fade-left, fade-right). Default is 0.5em. |