Back

Origami-Inspired Transformer Toggle Button

Discover how to craft a button that unfolds layers like a piece of origami every time it toggles, revealing hidden surprises with each transition.

Component Explanation

Origami-Inspired Transformer Toggle Button

The origami art form, known for its precise folds and delicate transformations, inspired us to design a button that mimics this enchanting process. In this tutorial, you’ll learn to build an interactive toggle button that unfolds like origami upon activation, offering a visually captivating experience.

HTML

The HTML structure is simple, comprising a <div> to center the button on the page and a button element for interaction. A <span> within the button shows the toggle state. Here's how:

<div class="origami-container">
  <button aria-label="Toggle Origami State" onclick="transformOrigami()" class="origami-toggle-button">
    <span class="toggle-state" aria-live="polite">Folded</span>
  </button>
</div>

Line by Line Explanation:

  • <div class="origami-container"> uses a flexbox to center content vertically and horizontally.
  • The <button> element is interactive, with an 'aria-label' for accessibility. It triggers the transformation function upon clicking.
  • The <span> inside the button holds the toggle state, making every transition intelligible to users, particularly for assistive technologies.

JavaScript

Our JavaScript controls the toggle logic and visual transformations. When the transformOrigami function is called, the button responds to the new state by unfolding, akin to origami:

let origamiToggled = false;
function transformOrigami() {
  const button = document.querySelector('.origami-toggle-button');
  const stateText = document.querySelector('.toggle-state');

  origamiToggled = !origamiToggled;

  if (origamiToggled) {
    button.style.background = '#DAF7A6';
    stateText.textContent = 'Unfolded';
    button.classList.add('origami-unfold');
  } else {
    button.style.background = '#FF5733';
    stateText.textContent = 'Folded';
    button.classList.remove('origami-unfold');
  }
}

Explanation of the Code:

  • We define origamiToggled as a boolean variable to track the toggle state.
  • The transformOrigami function toggles the state variable and updates button styles to simulate an unfolding or folding motion, with updated background color and text.

CSS

The CSS forms the core of our visual design, particularly with animations that create the origami effect. We've created classes for button styling, ensuring smooth transitions and generous animations:

.origami-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.origami-toggle-button {
  border: none;
  padding: 20px;
  border-radius: 12px;
  color: white;
  font-size: 16px;
  font-weight: bold;
  transition: background 0.5s, transform 0.4s ease-in-out;
  cursor: pointer;
  background: #FF5733;
  box-shadow: 0 0 8px rgba(0, 0, 0, 0.2);
}
.origami-unfold {
  transform: rotateX(360deg) scale(1.2);
  box-shadow: 0 0 15px rgba(0, 0, 0, 0.4);
}
button:focus {
  outline: none;
  box-shadow: 0 0 0 3px rgba(255, 87, 51, 0.4);
}
@media (prefers-reduced-motion: reduce) {
  .origami-toggle-button {
    transition: none;
  }
}

Design Breakdown:

  • The .origami-container class uses flexbox to position items at the center, adapting beautifully to all screen sizes.
  • The .origami-toggle-button class provides a vibrant, easily recognizable button design, aided by transitioning background and scale transformations.
  • The .origami-unfold class triggers animations upon toggle, creating a 3D rotation effect reminiscent of paper origami unfolding.
  • Accessibility is served by the :focus style and media query to respect user preference for reduced motion.

This project stands as a testament to how thoughtful animations and accessibility features can transform a simple button into an engaging user experience, weaving together art and interaction in captivating ways.

Whole code

<div class="origami-container">
  <button aria-label="Toggle Origami State" onclick="transformOrigami()" class="origami-toggle-button">
    <span class="toggle-state" aria-live="polite">Folded</span>
  </button>
</div>


<script>
let origamiToggled = false;
function transformOrigami() {
  const button = document.querySelector('.origami-toggle-button');
  const stateText = document.querySelector('.toggle-state');
  origamiToggled = !origamiToggled;
  if (origamiToggled) {
    button.style.background = '#DAF7A6';
    stateText.textContent = 'Unfolded';
    button.classList.add('origami-unfold');
  } else {
    button.style.background = '#FF5733';
    stateText.textContent = 'Folded';
    button.classList.remove('origami-unfold');
  }
}
</script>

<style>
.origami-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.origami-toggle-button {
  border: none;
  padding: 20px;
  border-radius: 12px;
  color: white;
  font-size: 16px;
  font-weight: bold;
  transition: background 0.5s, transform 0.4s ease-in-out;
  cursor: pointer;
  background: #FF5733;
  box-shadow: 0 0 8px rgba(0, 0, 0, 0.2);
}
.origami-unfold {
  transform: rotateX(360deg) scale(1.2);
  box-shadow: 0 0 15px rgba(0, 0, 0, 0.4);
}
button:focus {
  outline: none;
  box-shadow: 0 0 0 3px rgba(255, 87, 51, 0.4);
}
@media (prefers-reduced-motion: reduce) {
  .origami-toggle-button {
    transition: none;
  }
}
</style>
      
Thank you for reading this article.

Comments

More toggle-buttons

Similar

See also