Back

Crafting an Emotionally Responsive Mood Ring Button

Discover the magic of a button that changes colors based on user interaction moods inspired by the nostalgia of mood rings.

Component Explanation

Crafting an Emotionally Responsive Mood Ring Button

Have you ever wished for a UI component that not only performs a function but also resonates with nostalgia? Enter the Mood Ring Button—a button that changes its color scheme based on the user's mood, inspired by the classic mood ring. In this tutorial, we'll walk through how to create this delightful button using HTML, CSS, and JavaScript.

HTML Structure

This button resides in a straightforward HTML structure. We have a main container hosting our button. The button element contains a span to dynamically show the current mood state. Here's a breakdown of the HTML:

    <main class="mood-ring-container">
      <button onclick="toggleMood()" class="mood-ring-button" aria-label="Interactive Mood Ring Button">
        <span class="mood-state">Calm</span>
      </button>
    </main>
  

The <main> tag is semantically appropriate for enclosing the primary interactive content. Our button is equipped with an aria-label for accessibility, providing screen readers with a textual description of its purpose.

JavaScript Logic

Our JavaScript code handles mood toggling through an array of mood objects, each with a name and a corresponding color gradient. Each click cycles to the next mood. Let's dive into the JavaScript:

    let moods = [
      { name: 'Calm', color: 'linear-gradient(45deg, #00c6ff, #0072ff)' },
      { name: 'Excited', color: 'linear-gradient(45deg, #f857a6, #ff5858)' },
      { name: 'Curious', color: 'linear-gradient(45deg, #7f00ff, #e100ff)' },
    ];
    let currentMoodIndex = 0;
    function toggleMood() {
      let button = document.querySelector('.mood-ring-button');
      let stateText = document.querySelector('.mood-state');
      currentMoodIndex = (currentMoodIndex + 1) % moods.length;
      button.style.background = moods[currentMoodIndex].color;
      stateText.textContent = moods[currentMoodIndex].name;
    }
  

Using the modulo operator % ensures we cycle continuously through moods. This simple yet effective script demonstrates how JavaScript can breathe life into UI components.

Styling with CSS

The CSS of the Mood Ring Button utilizes modern gradients and transitions to create an engaging, tactile effect. Each mood is embodied by a unique linear gradient. Below is the CSS:

    .mood-ring-container {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
    }
    .mood-ring-button {
      border: none;
      padding: 20px 40px;
      border-radius: 50px;
      color: white;
      font-size: 20px;
      font-weight: bold;
      transition: background 0.4s ease, transform 0.4s ease;
      cursor: pointer;
      background: linear-gradient(45deg, #00c6ff, #0072ff);
      box-shadow: 0 8px 15px rgba(0, 0, 0, 0.1);
    }
    .mood-ring-button:hover {
      transform: translateY(-3px);
    }
    .mood-ring-button:focus {
      outline: 2px solid rgba(0,123,255,0.5);
      outline-offset: 4px;
    }
  

The button's hover effect enhances interactivity, drawing users in with a subtle lift. The focus style ensures keyboard users have a clear visual indicator of button interaction.

Conclusion

With just a touch of creativity, we've transformed a basic button into a nostalgic piece that harks back to the captivating nature of mood rings. This Mood Ring Button is not only aesthetically pleasing but offers a magical interaction experience as well.

Whole code

<main class="mood-ring-container">
  <button onclick="toggleMood()" class="mood-ring-button" aria-label="Interactive Mood Ring Button">
    <span class="mood-state">Calm</span>
  </button>
</main>

<script>
let moods = [
  { name: 'Calm', color: 'linear-gradient(45deg, #00c6ff, #0072ff)' },
  { name: 'Excited', color: 'linear-gradient(45deg, #f857a6, #ff5858)' },
  { name: 'Curious', color: 'linear-gradient(45deg, #7f00ff, #e100ff)' },
];
let currentMoodIndex = 0;
function toggleMood() {
  let button = document.querySelector('.mood-ring-button');
  let stateText = document.querySelector('.mood-state');
  currentMoodIndex = (currentMoodIndex + 1) % moods.length;
  button.style.background = moods[currentMoodIndex].color;
  stateText.textContent = moods[currentMoodIndex].name;
}
</script>

<style>
.mood-ring-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.mood-ring-button {
  border: none;
  padding: 20px 40px;
  border-radius: 50px;
  color: white;
  font-size: 20px;
  font-weight: bold;
  transition: background 0.4s ease, transform 0.4s ease;
  cursor: pointer;
  background: linear-gradient(45deg, #00c6ff, #0072ff);
  box-shadow: 0 8px 15px rgba(0, 0, 0, 0.1);
}
.mood-ring-button:hover {
  transform: translateY(-3px);
}
.mood-ring-button:focus {
  outline: 2px solid rgba(0,123,255,0.5);
  outline-offset: 4px;
}
</style>
      
Thank you for reading this article.

Comments

No comments yet. Be the first to comment!

More buttons

Similar

See also