webdevpuneet.com

Pull Cord Dark Mode Toggle: GSAP Draggable Lamp Switch

Pull Cord Dark Mode Toggle: GSAP Draggable Lamp Switch · Buttons · Plain HTML, CSS & JS · Live preview

CategoryButtons

Copy & paste

Source Code

<main class="pc-scene" id="pcScene" data-theme="dark">
  <div class="pc-lamp">
    <div class="pc-wire"></div>
    <div class="pc-shade"><span class="pc-bulb"></span></div>
    <div class="pc-glow"></div>
    <div class="pc-cord">
      <div class="pc-line" id="pcLine"></div>
      <button type="button" class="pc-bead" id="pcBead" role="switch" aria-checked="false" aria-label="Lamp and theme: pull the cord or press Enter to switch"></button>
    </div>
  </div>
  <article class="pc-note">
    <p class="pc-kicker" id="pcState">Lamp off &middot; dark theme</p>
    <h1>Night owl or early bird?</h1>
    <p>Pull the cord down to switch the lamp on. The page changes theme with it. It also works with Enter or Space on the bead.</p>
    <p class="pc-hint" id="pcHint">Drag the bead down to start</p>
  </article>
</main>

Extend with AI

Take This Snippet Further With AI

Paste this snippet into an AI coding assistant and ask it to explain how Draggable, a scaled cord and an elastic tween combine into a pull-chain switch, and how data-theme with CSS variables flips the whole page. Good follow-ups: save the theme in localStorage with no flash on load, add a soft click sound, or follow the user's prefers-color-scheme on first visit. Ask it to keep the keyboard and aria-checked behaviour.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a "pull cord dark mode toggle" in plain HTML, CSS and JavaScript using GSAP and its Draggable plugin (from a CDN).

Requirements:
- A scene with CSS variables for background, card, text, muted text and border, and a data-theme attribute ("dark" by default, "light" when on). Include a hanging lamp (wire, shade, bulb, a radial-gradient glow whose opacity follows a variable) and a content card with a state label, a heading, a paragraph and a hint.
- The cord is a thin div with transform-origin top, and the bead is a round button positioned at its end. Make the bead draggable with Draggable (type y, bounds from 0 to 90). While dragging, scale the cord's scaleY so it always reaches the bead.
- On release, if the bead moved at least about 44px, toggle the theme; in every case spring the bead back to y 0 with ease elastic.out(1, 0.3), updating the cord during the tween.
- The bead must be a real button with role="switch" and aria-checked updated on each toggle. Enter and Space (and a simple tap) should play a short tug then toggle. Show a visible focus ring.
- Update the state label text, hide the hint after the first toggle, and transition the theme colours.
- Respect prefers-reduced-motion by removing the spring and the colour transitions.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

What's included

Features

Draggable cord
GSAP Draggable limits the bead to vertical movement along the cord.
Stretching line
The cord scales in step with the bead so it always stays attached.
Elastic recoil
elastic.out gives the springy return of a real pull chain.
Pull threshold
Only a deliberate pull flips the theme, so accidental touches do nothing.
Lamp glow
A radial-gradient glow and a lit bulb fade in with the light theme.
CSS-variable theming
One data-theme attribute switches every colour on the page.
Real switch semantics
A button with role switch and a live aria-checked value, plus Enter and Space.
Reduced-motion safe
Transitions and the spring are removed when motion is reduced.

About this UI Snippet

Pull Cord Dark Mode Toggle: Drag a Lamp Cord to Switch Theme

Screenshot of the Pull Cord Dark Mode Toggle: GSAP Draggable Lamp Switch snippet rendered live

A theme switch is usually a small pill in the header that nobody notices. This one is a hanging lamp with a cord and a brass bead. Drag the bead down and the cord stretches; pull it far enough and the lamp clicks on, the glow fades in and the whole page flips from dark to light. Let go and the bead springs back with a wobble.

Draggable handles the pull

GSAP's Draggable is created on the bead with type: 'y' and bounds: { minY: 0, maxY: 90 }, so the bead can only travel straight down along the cord. There is no pointer-event code to write, and touch works the same as a mouse.

The cord is a stretched div

A thin div has transform-origin: top. On every drag update, stretch() sets its scaleY to (90 + y) / 90, so the line always meets the bead. Only transforms change, which keeps the interaction smooth.

A threshold decides the click

In onRelease, if the bead travelled at least 44px the theme toggles; a shorter pull does nothing. Either way the bead recoils with ease: 'elastic.out(1, 0.3)', which gives the springy overshoot of a real pull chain.

Theme through CSS variables

toggle() only sets data-theme on the scene. All colours are CSS custom properties redefined for [data-theme=light], including the glow's opacity, so the page, card, bulb and glow transition together. To drive a whole site, move the attribute to <html> and store the choice in localStorage.

Accessible by design

The bead is a real <button role="switch"> with aria-checked updated on every change and a clear label. Enter and Space give the cord a short tug and toggle, a tap does the same, and visible focus is styled. With prefers-reduced-motion the spring and transitions are removed. Compare it with the circular reveal theme toggle or build on it with GSAP Draggable inertia.

Step by step

How to Use

  1. 1
    Add the GSAP CDNsInclude gsap and Draggable from the CDN panel.
  2. 2
    Paste HTML, CSS and JSA hanging lamp with a cord and a bead appears beside a content card.
  3. 3
    Drag the bead downThe cord stretches; pulling past about 44px switches the lamp and theme.
  4. 4
    ReleaseThe bead springs back to rest with an elastic overshoot.
  5. 5
    Try the keyboardTab to the bead and press Enter or Space for a tug and toggle.
  6. 6
    Apply it to your siteMove data-theme to the html element and save the choice in localStorage.

Real-world uses

Common Use Cases

Portfolio and personal sites
Give a developer or designer site a theme switch with personality, one that visitors tend to play with before they read anything else.
Blog and documentation themes
Offer light and dark reading modes with a toggle that is fun but still keyboard accessible and clearly labelled.
Lighting, furniture and home brands
Make a lamp shop or interiors page demonstrate its product in the page itself, then continue into a cursor spotlight reveal.
Game and cozy-app landing pages
Set a mood for a night-time or reading-nook product by letting visitors turn the light on themselves.

Got questions?

Frequently Asked Questions

Draggable moves the bead along Y. When the bead is released, onRelease checks whether it travelled at least the pull distance. If so, toggle() flips the data-theme attribute and updates aria-checked. Then a GSAP tween returns the bead to y 0 with an elastic ease.

Yes. The bead is a button with role switch. Focusing it and pressing Enter or Space runs the same tug animation and toggle. aria-checked reflects the lamp state, and a focus ring is shown. Screen reader users get a labelled on and off switch.

Set the attribute on document.documentElement instead of the scene, then save the value with localStorage in toggle(). Read it back before first paint, ideally with a tiny inline script in the head, to avoid a flash of the wrong theme.

Yes. The PULL constant is the distance in pixels, and maxY in the bounds sets how far the bead can travel. Make PULL smaller for a lighter touch or larger for a firmer pull. LINE must match the cord's resting height in CSS.