<div class="morphing-demo">
<div class="button-container">
<button class="morph-btn" data-state="idle">
<span class="btn-text">Click to Process</span>
<span class="btn-loader">
<svg class="circular" viewBox="25 25 50 50">
<circle class="path" cx="50" cy="50" r="20" fill="none" stroke-width="2"></circle>
</svg>
</span>
<span class="btn-success">✓</span>
<span class="btn-error">✗</span>
</button>
<div class="btn-examples">
<button class="morph-btn secondary" data-state="idle">
<span class="btn-text">Secondary Action</span>
<span class="btn-loader">
<svg class="circular" viewBox="25 25 50 50">
<circle class="path" cx="50" cy="50" r="20" fill="none" stroke-width="2"></circle>
</svg>
</span>
<span class="btn-success">✓</span>
<span class="btn-error">✗</span>
</button>
<button class="morph-btn outline" data-state="idle">
<span class="btn-text">Outline Style</span>
<span class="btn-loader">
<svg class="circular" viewBox="25 25 50 50">
<circle class="path" cx="50" cy="50" r="20" fill="none" stroke-width="2"></circle>
</svg>
</span>
<span class="btn-success">✓</span>
<span class="btn-error">✗</span>
</button>
</div>
</div>
</div>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
.morphing-demo {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
font-family: 'Inter', sans-serif;
padding: 2rem;
}
.button-container {
display: flex;
flex-direction: column;
gap: 2rem;
align-items: center;
}
.morph-btn {
position: relative;
padding: 1rem 2.5rem;
background: linear-gradient(135deg, #667eea, #764ba2);
color: white;
border: none;
border-radius: 50px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
overflow: hidden;
min-width: 200px;
height: 60px;
display: flex;
align-items: center;
justify-content: center;
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}
.morph-btn:hover {
transform: translateY(-2px);
box-shadow: 0 10px 25px rgba(102, 126, 234, 0.4);
}
.morph-btn:active {
transform: translateY(0);
}
.morph-btn.secondary {
background: linear-gradient(135deg, #764ba2, #667eea);
}
.morph-btn.outline {
background: transparent;
border: 2px solid white;
color: white;
}
.morph-btn.outline:hover {
background: rgba(255, 255, 255, 0.1);
}
.btn-text,
.btn-loader,
.btn-success,
.btn-error {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.btn-text {
opacity: 1;
visibility: visible;
}
.btn-loader,
.btn-success,
.btn-error {
opacity: 0;
visibility: hidden;
}
/* Loading state */
.morph-btn[data-state="loading"] {
width: 60px;
border-radius: 50%;
}
.morph-btn[data-state="loading"] .btn-text {
opacity: 0;
visibility: hidden;
transform: translate(-50%, -50%) scale(0.8);
}
.morph-btn[data-state="loading"] .btn-loader {
opacity: 1;
visibility: visible;
}
/* Success state */
.morph-btn[data-state="success"] {
background: linear-gradient(135deg, #4ade80, #22c55e);
width: 60px;
border-radius: 50%;
}
.morph-btn[data-state="success"] .btn-text,
.morph-btn[data-state="success"] .btn-loader {
opacity: 0;
visibility: hidden;
}
.morph-btn[data-state="success"] .btn-success {
opacity: 1;
visibility: visible;
font-size: 1.5rem;
font-weight: bold;
}
/* Error state */
.morph-btn[data-state="error"] {
background: linear-gradient(135deg, #ef4444, #dc2626);
width: 60px;
border-radius: 50%;
}
.morph-btn[data-state="error"] .btn-text,
.morph-btn[data-state="error"] .btn-loader {
opacity: 0;
visibility: hidden;
}
.morph-btn[data-state="error"] .btn-error {
opacity: 1;
visibility: visible;
font-size: 1.5rem;
font-weight: bold;
}
/* Loader animation */
.circular {
width: 24px;
height: 24px;
animation: rotate 2s linear infinite;
}
.path {
stroke: currentColor;
stroke-linecap: round;
animation: dash 1.5s ease-in-out infinite;
}
@keyframes rotate {
100% {
transform: rotate(360deg);
}
}
@keyframes dash {
0% {
stroke-dasharray: 1, 150;
stroke-dashoffset: 0;
}
50% {
stroke-dasharray: 90, 150;
stroke-dashoffset: -35;
}
100% {
stroke-dasharray: 90, 150;
stroke-dashoffset: -124;
}
}
.btn-examples {
display: flex;
gap: 1rem;
flex-wrap: wrap;
justify-content: center;
}
@media (max-width: 768px) {
.morphing-demo {
padding: 1rem;
}
.btn-examples {
flex-direction: column;
align-items: center;
}
.morph-btn {
min-width: 180px;
height: 50px;
font-size: 0.9rem;
}
}
class MorphingButtonSystem {
constructor() {
this.buttons = document.querySelectorAll('.morph-btn');
this.init();
}
init() {
this.buttons.forEach(button => {
button.addEventListener('click', (e) => this.handleButtonClick(e));
});
}
handleButtonClick(event) {
const button = event.currentTarget;
const currentState = button.dataset.state;
if (currentState === 'idle') {
this.startProcess(button);
} else if (currentState === 'success' || currentState === 'error') {
this.resetButton(button);
}
}
startProcess(button) {
// Set loading state
this.setState(button, 'loading');
// Simulate async process
const duration = Math.random() * 2000 + 1000; // 1-3 seconds
const success = Math.random() > 0.3; // 70% success rate
setTimeout(() => {
if (success) {
this.setState(button, 'success');
// Auto reset after 2 seconds
setTimeout(() => this.resetButton(button), 2000);
} else {
this.setState(button, 'error');
// Auto reset after 2 seconds
setTimeout(() => this.resetButton(button), 2000);
}
}, duration);
}
setState(button, state) {
button.dataset.state = state;
// Add ripple effect for state changes
this.addRippleEffect(button);
// Trigger haptic feedback if available
if (navigator.vibrate) {
if (state === 'success') {
navigator.vibrate([50, 50, 50]);
} else if (state === 'error') {
navigator.vibrate([100, 50, 100]);
}
}
}
resetButton(button) {
this.setState(button, 'idle');
}
addRippleEffect(button) {
const ripple = document.createElement('span');
const rect = button.getBoundingClientRect();
const size = Math.max(rect.width, rect.height);
const x = rect.width / 2;
const y = rect.height / 2;
ripple.style.cssText = `
position: absolute;
border-radius: 50%;
background: rgba(255, 255, 255, 0.3);
transform: scale(0);
animation: ripple 0.6s linear;
left: ${x - size / 2}px;
top: ${y - size / 2}px;
width: ${size}px;
height: ${size}px;
pointer-events: none;
`;
button.appendChild(ripple);
setTimeout(() => {
ripple.remove();
}, 600);
}
}
// Add ripple animation CSS
const style = document.createElement('style');
style.textContent = `
@keyframes ripple {
to {
transform: scale(4);
opacity: 0;
}
}
`;
document.head.appendChild(style);
// Initialize when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
new MorphingButtonSystem();
});