Back

Unleashing the Dream Factory Morph Button

Discover an enchanting button that morphs like a dream, blending tactile fluid motion with a whimsical touch of claymorphism.

Component Explanation

Unleashing the Dream Factory Morph Button


In this tutorial, we're going to build a button that transforms with each interaction—just like a dream that morphs with your imagination. Unlike traditional buttons, this 'Dream Factory Morph Button' offers a soft claymorphic experience that evolves with a touch, making it both engaging and delightful to use. With this guide, you'll learn to create this from scratch, employing modern CSS techniques and ensuring accessibility compliance throughout.

HTML Structure for the Morph Button

We start with defining a simple HTML structure. This includes a container for our button, equipped with an aria-label for accessibility. The 'Dream Factory Morph Button' animates between two states, and the HTML showcases how this structure supports interactivity seamlessly.

  <div class="dream-factory-container">
    <button aria-label="Activate Dream Mode" onclick="toggleDreamMode()" class="dream-button">
      <span class="dream-state">Awaken</span>
    </button>
  </div>

Here, the <div> serves as a centralizer using flexbox, while the <button> element includes an aria-label that succinctly communicates the action to screen readers.

Javascript Logic for Toggling States

Our JavaScript manages the toggle action, flipping between 'Awaken' and 'Dream' states. The color and shadow also change—a nod to claymorphism.

  let dreamModeActive = false;

  function toggleDreamMode() {
    const button = document.querySelector('.dream-button');
    const stateText = document.querySelector('.dream-state');

    dreamModeActive = !dreamModeActive;

    if (dreamModeActive) {
      button.style.background = "linear-gradient(135deg, #ff9a9e 0%, #fad0c4 100%)";
      stateText.textContent = "Dream";
      button.classList.add('dream-active');
    } else {
      button.style.background = "linear-gradient(135deg, #a1c4fd 0%, #c2e9fb 100%)";
      stateText.textContent = "Awaken";
      button.classList.remove('dream-active');
    }
  }

In this code, we ensure a seamless switch by manipulating text and style properties, providing visual feedback to the user with every click.

CSS Styling: Tactile Claymorphic Design

Incorporating modern design trends like claymorphism, the CSS adds a unique tactile quality to our button. Below is how we style it to resemble a morphing dream state, complete with visually appealing shadows and transitions.

  .dream-factory-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
  }

  .dream-button {
    border: none;
    padding: 20px 40px;
    border-radius: 10px;
    color: white;
    font-size: 20px;
    font-weight: bold;
    transition: background 0.6s, transform 0.5s, box-shadow 0.5s ease-in-out;
    cursor: pointer;
    background: linear-gradient(135deg, #a1c4fd 0%, #c2e9fb 100%);
    box-shadow: 10px 10px 20px rgba(0,0,0,0.2);
  }

  .dream-active {
    transform: scale(1.2);
    box-shadow: 0 10px 20px rgba(255,154,158,0.6), 0 0 30px rgba(250,208,196,0.6);
  }

  @media (prefers-reduced-motion: reduce) {
    .dream-button {
      transition: none;
    }
  }

We employ .dream-factory-container for a simple yet effective centering method using flexbox. The .dream-button holds our stylistic elements, detailing background gradient transitions and scaling effects for a morphing illusion. Note the use of media queries to respect user preferences for reduced motion, ensuring accessibility.

This walkthrough has guided you through creating a whimsical button, stepping beyond standard UI practice and into the sphere of playful, interactive web components. Not only is it functional, adhering to accessibility standards, but also visually engaging, thanks to its morphing, dreamlike qualities.

Whole code

<div class="dream-factory-container">
  <button aria-label="Activate Dream Mode" onclick="toggleDreamMode()" class="dream-button">
    <span class="dream-state">Awaken</span>
  </button>
</div>


<script>
let dreamModeActive = false;
function toggleDreamMode() {
  const button = document.querySelector('.dream-button');
  const stateText = document.querySelector('.dream-state');
  dreamModeActive = !dreamModeActive;
  if (dreamModeActive) {
    button.style.background = "linear-gradient(135deg, #ff9a9e 0%, #fad0c4 100%)";
    stateText.textContent = "Dream";
    button.classList.add('dream-active');
  } else {
    button.style.background = "linear-gradient(135deg, #a1c4fd 0%, #c2e9fb 100%)";
    stateText.textContent = "Awaken";
    button.classList.remove('dream-active');
  }
}
</script>

<style>
.dream-factory-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.dream-button {
  border: none;
  padding: 20px 40px;
  border-radius: 10px;
  color: white;
  font-size: 20px;
  font-weight: bold;
  transition: background 0.6s, transform 0.5s, box-shadow 0.5s ease-in-out;
  cursor: pointer;
  background: linear-gradient(135deg, #a1c4fd 0%, #c2e9fb 100%);
  box-shadow: 10px 10px 20px rgba(0,0,0,0.2);
}
.dream-active {
  transform: scale(1.2);
  box-shadow: 0 10px 20px rgba(255,154,158,0.6), 0 0 30px rgba(250,208,196,0.6);
}
@media (prefers-reduced-motion: reduce) {
  .dream-button {
    transition: none;
  }
}
</style>
      
Thank you for reading this article.

Comments

More buttons

Similar

See also