Back
Delve into a whimsical button design that mimics a puzzle piece, encouraging users to 'connect' its parts through interaction.
In this detailed tutorial, we embark on a playful journey of creating a novel UI component inspired by a conventional puzzle piece. This button not only serves a functional need but also sparks curiosity and enjoyment through its design and interactivity.
Let's begin by crafting the HTML structure. We utilize a simple, semantic format to ensure accessibility and SEO adherence. Here's the breakdown:
<div class="puzzle-piece-container">
<button onclick="toggleFit()" class="puzzle-piece-fit-button" aria-label="Toggle puzzle piece fit state">
<span class="puzzle-fit-state">DETACHED</span>
</button>
</div>
The HTML code above incorporates a div that centers the button within the viewport. The button element, mimicking a puzzle piece, toggles between 'DETACHED' and 'ATTACHED' states when clicked.
We write a Javascript function that toggles the fit state of our puzzle piece button. It's a simple yet effective way to enhance interactivity.
let isFitted = false;
function toggleFit() {
let button = document.querySelector('.puzzle-piece-fit-button');
let stateText = document.querySelector('.puzzle-fit-state');
isFitted = !isFitted;
if (isFitted) {
stateText.textContent = "ATTACHED";
button.classList.add('piece-attached');
} else {
stateText.textContent = "DETACHED";
button.classList.remove('piece-attached');
}
}
The function and event listener modify both the display text and CSS class depending on the component’s state, achieving an engaging user interaction.
The CSS is where the magic truly unfolds, transforming our button into a charm reminiscent of a real puzzle piece.
.puzzle-piece-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.puzzle-piece-fit-button {
border: none;
padding: 20px;
border-radius: 25% 75% 75% 25% / 30% 60% 40% 70%;
font-size: 18px;
font-weight: bold;
transition: transform 0.5s, box-shadow 0.4s;
cursor: pointer;
background: #8c9eff;
color: #ffffff;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
.piece-attached {
transform: rotate(15deg);
box-shadow: 0 8px 12px rgba(0,0,255,0.3);
}
Here, we employ an ingenious border-radius to mimic the contours of a puzzle piece. The playful rotations and dynamic shadow effects add a tactile sense of motion, capturing user delight.
In conclusion, this UI component demonstrates that by drawing inspiration from everyday objects—like a puzzle piece—one can infuse traditional elements with an element of play and surprise. Not only does it serve its functional purpose clearly, but it also invites engagement, aesthetically elevating minimal UI principles into the realm of interaction art.
<div class="puzzle-piece-container">
<button onclick="toggleFit()" class="puzzle-piece-fit-button" aria-label="Toggle puzzle piece fit state">
<span class="puzzle-fit-state">DETACHED</span>
</button>
</div>
<script>
let isFitted = false;
function toggleFit() {
let button = document.querySelector('.puzzle-piece-fit-button');
let stateText = document.querySelector('.puzzle-fit-state');
isFitted = !isFitted;
if (isFitted) {
stateText.textContent = "ATTACHED";
button.classList.add('piece-attached');
} else {
stateText.textContent = "DETACHED";
button.classList.remove('piece-attached');
}
}
</script>
<style>
.puzzle-piece-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.puzzle-piece-fit-button {
border: none;
padding: 20px;
border-radius: 25% 75% 75% 25% / 30% 60% 40% 70%;
font-size: 18px;
font-weight: bold;
transition: transform 0.5s, box-shadow 0.4s;
cursor: pointer;
background: #8c9eff;
color: #ffffff;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
.piece-attached {
transform: rotate(15deg);
box-shadow: 0 8px 12px rgba(0,0,255,0.3);
}
</style>
Thank you for reading this article.
Comments