Back

Building a Sandcastle Input for Whimsical User Interaction

Discover how to create a magical input field that morphs like a sandcastle transforming and rebuilding itself with interactive claymorphic visuals.

Component Explanation

Creating an Interactive Sandcastle Input

Welcome to this tutorial where you'll learn how to create a unique input field that behaves like a whimsical sandcastle. Using modern CSS effects like claymorphism and clever JavaScript interactions, we will build an input component that transforms and animates just like magic.

HTML Structure

First, we'll start with the HTML setup. The div wraps our input component ensuring it remains centered on the page.

<div class="sandcastle-container">
  <label for="name-input" class="sr-only">Enter your name</label>
  <input id="name-input" aria-label="Enter your name" placeholder="Enter your name" class="sandcastle-input" type="text" />
</div>

Adding Visual Interest with CSS

Now, dive into CSS to design our input field's dynamic appearance. We're applying a claymorphic design with soft edges and depth.

.sandcastle-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.sandcastle-input {
  width: 80%;
  padding: 15px;
  border-radius: 12px;
  border: 2px solid transparent;
  background: rgba(255, 234, 207, 0.8);
  box-shadow: inset 0 0 5px rgba(204, 163, 128, 0.5);
  transition: all 0.3s ease-out;
  font-size: 16px;
}
.sculpt-focus {
  background: rgba(255, 213, 153, 0.8);
  box-shadow: inset 0 0 10px rgba(204, 163, 128, 0.8), 0 0 10px rgba(255, 213, 153, 0.9);
  transform: scale(1.02);
  border-color: rgba(204, 163, 128, 0.9);
}
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

Interactive Dynamics with JavaScript

To elevate the user experience, we add a subtle input focus interaction using JavaScript. This will animate the field to feel like it is rebuilding itself from sand.

const input = document.querySelector('.sandcastle-input');
input.addEventListener('focus', () => {
  input.classList.add('sculpt-focus');
});
input.addEventListener('blur', () => {
  input.classList.remove('sculpt-focus');
});

Making it Accessible and SEO-Friendly

Finally, let's optimize for accessibility. We use aria-label for the input to ensure screen readers can describe its purpose. The focus state ensures keyboard users can also enjoy a clear visual indication.

Through this tutorial, we've created an interactive input that not only serves its functional purpose but also brings an element of playful design. Try experimenting with different colors and transitions to make it your own!

Whole code

<div class="sandcastle-container">
  <label for="name-input" class="sr-only">Enter your name</label>
  <input id="name-input" aria-label="Enter your name" placeholder="Enter your name" class="sandcastle-input" type="text" />
</div>


<script>
const input = document.querySelector('.sandcastle-input');
input.addEventListener('focus', () => {
  input.classList.add('sculpt-focus');
});
input.addEventListener('blur', () => {
  input.classList.remove('sculpt-focus');
});
</script>

<style>
.sandcastle-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.sandcastle-input {
  width: 80%;
  padding: 15px;
  border-radius: 12px;
  border: 2px solid transparent;
  background: rgba(255, 234, 207, 0.8);
  box-shadow: inset 0 0 5px rgba(204, 163, 128, 0.5);
  transition: all 0.3s ease-out;
  font-size: 16px;
}
.sculpt-focus {
  background: rgba(255, 213, 153, 0.8);
  box-shadow: inset 0 0 10px rgba(204, 163, 128, 0.8), 0 0 10px rgba(255, 213, 153, 0.9);
  transform: scale(1.02);
  border-color: rgba(204, 163, 128, 0.9);
}
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}
</style>
      
Thank you for reading this article.

Comments

No comments yet. Be the first to comment!

More inputs

Similar

See also