Skip to content

ComponentsFormatting

SourceSource

Spoiler

Inline text hidden behind a solid block until the reader clicks to reveal it.

Inline text hidden behind a solid block until the reader clicks to reveal it. Everything inside is hidden while it is closed, links, inline code, mentions and emoji images included, and none of it can be clicked or reached with Tab until the reader reveals it. Compose it from the exported registry parts so local content and behaviour stay in the application while the visual contract stays token-driven.

Root class newt-spoiler
import { Spoiler } from "@/registry/bases/newt/ui/spoiler"

export default function SpoilerDemo() {
  return (
    <p className="text-sm text-newt-text-primary">
      The ending was <Spoiler>hidden text</Spoiler> all along.
    </p>
  )
}
<script setup lang="ts">
import { Spoiler } from "~~/registry/bases/newt/ui/spoiler"
</script>

<template>
  <p class="text-sm text-newt-text-primary">
    The ending was <Spoiler>hidden text</Spoiler> all along.
  </p>
</template>

Installation

npx newtui@latest add spoilernpx shadcn@latest add @newtui/spoilernpx shadcn-vue@latest add @newtui/spoilerpnpm dlx newtui@latest add spoilerpnpm dlx shadcn@latest add @newtui/spoilerpnpm dlx shadcn-vue@latest add @newtui/spoileryarn dlx newtui@latest add spoileryarn dlx shadcn@latest add @newtui/spoileryarn dlx shadcn-vue@latest add @newtui/spoilerbunx newtui@latest add spoilerbunx shadcn@latest add @newtui/spoilerbunx shadcn-vue@latest add @newtui/spoilerdeno run -A npm:newtui@latest add spoilerdeno run -A npm:shadcn@latest add @newtui/spoilerdeno run -A npm:shadcn-vue@latest add @newtui/spoilervlx newtui@latest add spoilervlx shadcn@latest add @newtui/spoilervlx shadcn-vue@latest add @newtui/spoilervp dlx newtui@latest add spoilervp dlx shadcn@latest add @newtui/spoilervp dlx shadcn-vue@latest add @newtui/spoilernubx newtui@latest add spoilernubx shadcn@latest add @newtui/spoilernubx shadcn-vue@latest add @newtui/spoilernlx newtui@latest add spoilernlx shadcn@latest add @newtui/spoilernlx shadcn-vue@latest add @newtui/spoilerupx newtui@latest add spoilerupx shadcn@latest add @newtui/spoilerupx shadcn-vue@latest add @newtui/spoiler

The shadcn CLI needs the @newtui registry in your components.json. Set it up.

Copy the source into your project

"use client"

import * as React from "react"

import { cn } from "@/lib/utils"

export interface SpoilerProps extends React.HTMLAttributes<HTMLSpanElement> {
  /** Controlled revealed state. */
  revealed?: boolean
  /** Initial revealed state when uncontrolled. */
  defaultRevealed?: boolean
  /** Called with the next revealed state when toggled. */
  onRevealedChange?: (revealed: boolean) => void
  /**
   * Accessible name while hidden, read in place of the content it hides.
   * Defaults to `"Spoiler, click to reveal"`; pass a translated string.
   */
  label?: string
}

const Spoiler = React.forwardRef<HTMLSpanElement, SpoilerProps>(
  (
    {
      className,
      revealed: revealedProp,
      defaultRevealed = false,
      onRevealedChange,
      label = "Spoiler, click to reveal",
      onClick,
      onKeyDown,
      children,
      ...props
    },
    ref
  ) => {
    const [uncontrolled, setUncontrolled] = React.useState(defaultRevealed)
    const isControlled = revealedProp !== undefined
    const revealed = isControlled ? revealedProp : uncontrolled

    const toggle = () => {
      const next = !revealed
      if (!isControlled) setUncontrolled(next)
      onRevealedChange?.(next)
    }

    const handleClick = (event: React.MouseEvent<HTMLSpanElement>) => {
      onClick?.(event)
      if (event.defaultPrevented) return
      toggle()
    }

    const handleKeyDown = (event: React.KeyboardEvent<HTMLSpanElement>) => {
      onKeyDown?.(event)
      if (event.defaultPrevented || revealed) return
      if (event.key === "Enter" || event.key === " ") {
        event.preventDefault()
        toggle()
      }
    }

    return (
      <span
        ref={ref}
        role={revealed ? undefined : "button"}
        tabIndex={revealed ? undefined : 0}
        aria-label={revealed ? undefined : label}
        aria-expanded={revealed}
        data-state={revealed ? "revealed" : "hidden"}
        className={cn(
          "rounded-[var(--newt-radius-sm)] px-1 transition-[background-color] duration-fast ease-newt",
          revealed
            ? "cursor-text select-text bg-newt-bg-active text-newt-text-primary"
            : "cursor-pointer select-none bg-newt-spoiler hover:bg-newt-spoiler-hover",
          className
        )}
        onClick={handleClick}
        onKeyDown={handleKeyDown}
        {...props}
      >
        {/* Hidden, the content is invisible and inert rather than painted in
         * the background colour: a link, an image or a child with its own
         * colour would otherwise show through, and a link could still be
         * clicked or tabbed to. Both keep its box, so the chip keeps the size
         * of what it hides. */}
        <span inert={!revealed} className={revealed ? undefined : "invisible"}>
          {children}
        </span>
      </span>
    )
  }
)
Spoiler.displayName = "Spoiler"

export { Spoiler }
<script setup lang="ts">
import type { HTMLAttributes } from "vue"
import { computed, ref, watch } from "vue"

import { cn } from "@/lib/utils"

const props = withDefaults(
  defineProps<{
    class?: HTMLAttributes["class"]
    /** Controlled revealed state (use with v-model:revealed). */
    revealed?: boolean
    /** Initial revealed state when uncontrolled. */
    defaultRevealed?: boolean
    /**
     * Accessible name while hidden, read in place of the content it hides.
     * Defaults to `"Spoiler, click to reveal"`; pass a translated string.
     */
    label?: string
  }>(),
  {
    revealed: undefined,
    defaultRevealed: false,
    label: "Spoiler, click to reveal",
  }
)

const emit = defineEmits<{ (e: "update:revealed", value: boolean): void }>()

const internal = ref(props.revealed ?? props.defaultRevealed)
watch(
  () => props.revealed,
  (v) => {
    if (v !== undefined) internal.value = v
  }
)
const isRevealed = computed(() =>
  props.revealed !== undefined ? props.revealed : internal.value
)

function toggle() {
  const next = !isRevealed.value
  if (props.revealed === undefined) internal.value = next
  emit("update:revealed", next)
}

function onKeydown(event: KeyboardEvent) {
  if (isRevealed.value) return
  if (event.key === "Enter" || event.key === " ") {
    event.preventDefault()
    toggle()
  }
}
</script>

<template>
  <span
    :role="isRevealed ? undefined : 'button'"
    :tabindex="isRevealed ? undefined : 0"
    :aria-label="isRevealed ? undefined : props.label"
    :aria-expanded="isRevealed"
    :data-state="isRevealed ? 'revealed' : 'hidden'"
    :class="
      cn(
        'rounded-[var(--newt-radius-sm)] px-1 transition-[background-color] duration-fast ease-newt',
        isRevealed
          ? 'cursor-text select-text bg-newt-bg-active text-newt-text-primary'
          : 'cursor-pointer select-none bg-newt-spoiler hover:bg-newt-spoiler-hover',
        props.class
      )
    "
    @click="toggle"
    @keydown="onKeydown"
  >
    <!--
      Hidden, the content is invisible and inert rather than painted in the
      background colour: a link, an image or a child with its own colour would
      otherwise show through, and a link could still be clicked or tabbed to.
      Both keep its box, so the chip keeps the size of what it hides.
    -->
    <span :inert="!isRevealed" :class="isRevealed ? undefined : 'invisible'"
      ><slot
    /></span>
  </span>
</template>
export { default as Spoiler } from "./Spoiler.vue"

Update the import paths

The files import @/lib/utils and sibling components through the aliases in your components.json. Point them at wherever those live in your project.

No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Spoiler to your React project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.

No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Spoiler to your Vue project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.

Usage

import { Spoiler } from "@/components/ui/spoiler"

export function Example() {
  return <Spoiler />
}
<script setup lang="ts">
import { Spoiler } from "@/components/ui/spoiler"
</script>

<template>
  <Spoiler />
</template>

API reference

Spoiler

PropType
revealed?boolean | undefined

Controlled revealed state.

Typeboolean | undefined
defaultRevealed?boolean | undefined

Initial revealed state when uncontrolled.

Typeboolean | undefined
onRevealedChange?(revealed: boolean) => void | undefined

Called with the next revealed state when toggled.

Type(revealed: boolean) => void | undefined
label?string | undefined

Accessible name while hidden, read in place of the content. Defaults to "Spoiler, click to reveal".

Typestring | undefined

The registry exports Spoiler. Parts without a table accept the native attributes for the element they render; React uses className, while Vue uses class and fallthrough attributes.

Accessibility

Hidden, the spoiler is a button in the tab order: Enter or Space reveals it, and aria-expanded reports its state. Its accessible name is label, so a screen reader announces the spoiler without reading what it hides; pass a translated label when the page is not in English. The content inside is inert and invisible until revealed, so a link in it cannot be followed by accident. Do not place essential instructions only inside hidden spoiler content. In the plain HTML and CSS flavour, load spoiler.js with the markup: it does the reveal, the keyboard handling and the inert toggle, so without it a spoiler can never be revealed.

Translating labels lists every English default.

The hidden chip is --newt-spoiler, and --newt-spoiler-hover under the pointer, each with a light and a dark value.

Tokens

Spoiler reads its colours, radii and motion from the --newt-* tokens installed with the library. Override a token and every component that reads it follows, including this one — see the token layer. In the plain HTML and CSS distribution the same component is newt-spoiler.

  • --newt-spoiler
  • --newt-radius-sm
  • --newt-duration-fast
  • --newt-ease
  • --newt-bg-active
  • --newt-text-primary
  • --newt-spoiler-hover

Changelog

  1. feataccessible and themed Spoiler, Modal and CodeBlock#57