modified toggle
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
:root {
|
||||
--bg-off: #fff;
|
||||
--bg-on: linear-gradient(90deg, #003e79 0%, #015cb8 100%);
|
||||
--thumb-off: #015cb8;
|
||||
--thumb-on: #fff;
|
||||
--shadow: 0 6px 18px rgba(99, 102, 241, 0.18);
|
||||
--curve: cubic-bezier(0.175, 0.885, 0.32, 1.275);
|
||||
}
|
||||
|
||||
.containerToggle{
|
||||
display: flex;
|
||||
justify-content: end;
|
||||
}
|
||||
|
||||
.toggle {
|
||||
--padding: 4px;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
|
||||
border: none;
|
||||
background: transparent;
|
||||
cursor: pointer;
|
||||
user-select: none;
|
||||
border-radius: 999px;
|
||||
transition: transform 160ms ease, box-shadow 160ms ease;
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.toggle:active .track {
|
||||
box-shadow: inset 0 0 0 2em rgba(0, 0, 0, 0.06);
|
||||
}
|
||||
|
||||
.track {
|
||||
width: 70px;
|
||||
height: 34px;
|
||||
background: var(--bg-off);
|
||||
border-radius: 999px;
|
||||
display: inline-block;
|
||||
padding: var(--padding);
|
||||
box-sizing: border-box;
|
||||
position: relative;
|
||||
transition: background 280ms var(--curve), box-shadow 220ms ease;
|
||||
border: 1px solid #e8eae9;
|
||||
}
|
||||
|
||||
.on .track {
|
||||
background: var(--bg-on);
|
||||
box-shadow: var(--shadow);
|
||||
border-color: transparent;
|
||||
}
|
||||
|
||||
.thumb {
|
||||
width: 23px;
|
||||
height: 23px;
|
||||
background: var(--thumb-off);
|
||||
border-radius: 50%;
|
||||
display: block;
|
||||
transform: translateX(0);
|
||||
transition: transform 260ms var(--curve), background 240ms ease,
|
||||
padding 260ms ease, margin 260ms ease, box-shadow 240ms ease;
|
||||
box-shadow: 0 3px 8px rgba(0, 0, 0, 0.12);
|
||||
}
|
||||
|
||||
/* Rebote estilo iOS */
|
||||
.toggle:active .thumb {
|
||||
padding-right: 4px;
|
||||
}
|
||||
|
||||
.on:active .thumb {
|
||||
margin-left: -4px;
|
||||
}
|
||||
|
||||
.on .thumb {
|
||||
background: var(--thumb-on);
|
||||
transform: translateX(36px);
|
||||
box-shadow: 0 6px 16px rgba(91, 90, 255, 0.18);
|
||||
}
|
||||
|
||||
.label {
|
||||
position: absolute;
|
||||
right: 10px;
|
||||
top: 8px;
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
color: #0f172a;
|
||||
letter-spacing: 0.2px;
|
||||
transition: color 240ms ease, right 260ms var(--curve);
|
||||
}
|
||||
|
||||
.on .label {
|
||||
right: 45%;
|
||||
color: #fff;
|
||||
text-shadow: 0 1px 0 rgba(0, 0, 0, 0.12);
|
||||
}
|
||||
|
||||
.toggle:focus-visible .track {
|
||||
box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.12);
|
||||
}
|
||||
|
||||
@media (max-width: 420px) {
|
||||
.track {
|
||||
width: 52px;
|
||||
height: 30px;
|
||||
}
|
||||
.thumb {
|
||||
width: 22px;
|
||||
height: 22px;
|
||||
}
|
||||
.on .thumb {
|
||||
transform: translateX(24px);
|
||||
}
|
||||
.label {
|
||||
font-size: 13px;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// ToggleButton.tsx
|
||||
// Usa este archivo como `ToggleButton.tsx`
|
||||
|
||||
import React, { useState } from "react";
|
||||
import styles from "./ToggleButton.module.scss";
|
||||
|
||||
export type ToggleButtonProps = {
|
||||
/** estado inicial: true -> presionado ("desuso") */
|
||||
defaultOn?: boolean;
|
||||
/** callback cuando cambia el estado */
|
||||
onChange?: (on: boolean) => void;
|
||||
/** texto cuando está apagado (por defecto: "uso") */
|
||||
offLabel?: string;
|
||||
/** texto cuando está encendido (por defecto: "desuso") */
|
||||
onLabel?: string;
|
||||
className?: string;
|
||||
};
|
||||
|
||||
const ToggleButton: React.FC<ToggleButtonProps> = ({
|
||||
defaultOn = false,
|
||||
onChange,
|
||||
offLabel = "Uso",
|
||||
onLabel = "Baja",
|
||||
className = "",
|
||||
}) => {
|
||||
const [on, setOn] = useState<boolean>(defaultOn);
|
||||
|
||||
function toggle() {
|
||||
const next = !on;
|
||||
setOn(next);
|
||||
onChange?.(next);
|
||||
}
|
||||
|
||||
return (
|
||||
<section className={styles.containerToggle}>
|
||||
<button
|
||||
type="button"
|
||||
aria-pressed={on}
|
||||
className={`${styles.toggle} ${on ? styles.on : ""} ${className}`}
|
||||
onClick={toggle}
|
||||
title={on ? onLabel : offLabel}
|
||||
>
|
||||
<span className={styles.track}>
|
||||
<span className={styles.thumb} />
|
||||
<span className={styles.label}>{on ? onLabel : offLabel}</span>
|
||||
</span>
|
||||
</button>
|
||||
</section>
|
||||
);
|
||||
};
|
||||
|
||||
export default ToggleButton;
|
||||
Reference in New Issue
Block a user