← Text reveals

Bracket Reveal

A pair of brackets appears pressed together, then slides apart, uncovering the word between them as they go, like a selection opening up.

Selected

How to use it

  1. Paste the snippet just before </head>.
  2. Add data-bracket-reveal to a word or short heading.

Options

Add any of these attributes to the same element.

data-bracketsThe two bracket characters. Default []; try {}, <> or ().
data-colorThe brackets’ colour. Default #a855f7.
data-durationTime to open. Default 900 ms.
data-loopOpen, hold, close up and open again.
data-holdHow long it stays open, when looping. Default 1800 ms.
data-delayWait before starting. Default 0 ms.
1 · Snippet
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
  (function () {
    // Bracket Reveal: a pair of brackets appears closed together in the middle,
    // then slides apart, uncovering the word between them as they go.
    // Usage: <h1 data-bracket-reveal>Selected</h1>
    // Optional: data-brackets (the two bracket characters, default "[]"; try
    // "{}", "<>" or "()"), data-color (the brackets, default "#a855f7"),
    // data-duration (ms to open, default 900), data-loop (close up and open
    // again), data-hold (ms to stay open, when looping), data-delay (ms).
    var DEFAULTS = { brackets: '[]', color: '#a855f7', duration: 900, hold: 1800, delay: 0 };

    var css =
      '.brr-wrap{display:inline-flex;align-items:center;max-width:100%;vertical-align:bottom}' +
      '.brr-b{flex:none;font-weight:300;line-height:1;transform:scaleY(1.15);will-change:transform;position:relative;z-index:1}' +
      '.brr-mid{min-width:0;padding:0 .08em}';
    var style = document.createElement('style');
    style.textContent = css;
    document.head.appendChild(style);

    function reduced() {
      return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
    }

    function setup(el) {
      var d = el.dataset;
      var text = el.textContent.replace(/\s+/g, ' ').trim();
      var pair = Array.from(d.brackets || DEFAULTS.brackets);
      el.setAttribute('aria-label', text);
      el.textContent = '';
      var wrap = document.createElement('span');
      wrap.className = 'brr-wrap';
      wrap.setAttribute('aria-hidden', 'true');
      function bracket(ch) {
        var b = document.createElement('span');
        b.className = 'brr-b';
        b.textContent = ch;
        b.style.color = d.color || DEFAULTS.color;
        return b;
      }
      var l = bracket(pair[0] || '['), r = bracket(pair[1] || pair[0] || ']');
      var mid = document.createElement('span');
      mid.className = 'brr-mid';
      mid.textContent = text;
      wrap.appendChild(l);
      wrap.appendChild(mid);
      wrap.appendChild(r);
      el.appendChild(wrap);
      return { l: l, r: r, mid: mid, k: 0 };
    }

    // k = 0: brackets together in the middle, word hidden; 1: fully open.
    function draw(s) {
      var half = s.mid.offsetWidth / 2, k = s.k, off = half * (1 - k);
      var cut = Math.max(0, 50 * (1 - k));
      s.l.style.transform = 'translateX(' + off.toFixed(1) + 'px) scaleY(1.15)';
      s.r.style.transform = 'translateX(' + (-off).toFixed(1) + 'px) scaleY(1.15)';
      s.mid.style.clipPath = 'inset(-20% ' + cut.toFixed(2) + '% -20% ' + cut.toFixed(2) + '%)';
      s.mid.style.opacity = Math.min(1, k * 1.6).toFixed(3);
    }

    function play(el) {
      var s = el._brr, d = el.dataset;
      var loop = d.loop !== undefined && d.loop !== 'false';
      var duration = +d.duration || DEFAULTS.duration;
      var tl = anime.createTimeline({
        onComplete: function () { if (loop) setTimeout(function () { play(el); }, 300); }
      });
      s.k = 0;
      draw(s);
      // The closed pair pops in, then opens.
      tl.add([s.l, s.r], { opacity: [0, 1], duration: 300, ease: 'outQuad' }, 0);
      tl.add(s, {
        k: [0, 1],
        duration: duration,
        ease: 'inOutCubic',
        onUpdate: function () { draw(s); }
      }, 350);
      if (loop) {
        tl.add(s, {
          k: [1, 0],
          duration: duration * 0.7,
          ease: 'inOutCubic',
          onUpdate: function () { draw(s); }
        }, '+=' + (+d.hold || DEFAULTS.hold));
        tl.add([s.l, s.r], { opacity: [1, 0], duration: 250, ease: 'inQuad' });
      }
    }

    function init() {
      var io = new IntersectionObserver(function (entries) {
        entries.forEach(function (e) {
          if (!e.isIntersecting) return;
          io.unobserve(e.target);
          var el = e.target;
          setTimeout(function () { play(el); }, +el.dataset.delay || DEFAULTS.delay);
        });
      }, { threshold: 0.3 });
      document.querySelectorAll('[data-bracket-reveal]').forEach(function (el) {
        if (el._brr) return;
        var s = el._brr = setup(el);
        if (reduced()) return;
        s.l.style.opacity = s.r.style.opacity = 0;
        s.mid.style.opacity = 0;
        io.observe(el);
      });
    }

    if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
    else init();
  })();
</script>
2 · Your HTML
<h1 data-bracket-reveal>Selected</h1>

<!-- or, with curly braces: -->
<h1 data-bracket-reveal data-brackets="{}">Selected</h1>