To make the <kbd> element pressable with a 3D effect that remains down once clicked, you can modify your code as follows:
-
Use a <kbd> Element: Replace your <button> element with a <kbd> element to achieve the desired look.
-
Track the Pressed State: Introduce a state variable (e.g., isPressed) to keep track of whether the key has been pressed.
-
Apply Conditional Styling: Use conditional classes and styles based on the isPressed state to create the 3D press effect and adjust the appearance.
Here's how you can implement it:
<script>
// Initialize the pressed state
let isPressed = false;
// Function to handle the press action
function handlePress() {
isPressed = true; // Set the state to pressed
// Add any additional logic here if needed
}
</script>
<!-- The pressable kbd element -->
<kbd
on:click={handlePress}
class="cursor-pointer select-none
transition-all duration-150
text-white font-bold text-lg
{isPressed
? 'translate-y-2 [box-shadow:0_0px_0_0_rgba(0,0,0,0)] border-b-0 bg-blue-600'
: '[box-shadow:0_10px_0_0_#1b6ff8] border-b border-blue-400 bg-blue-500'}"
style="{isPressed ? 'font-size: 20px' : 'font-size:18px'}">
A
</kbd>
Explanation:
-
Event Handling: The on:click event calls the handlePress function, which sets isPressed to true. This ensures that once the key is pressed, it remains in the pressed state.
-
Conditional Classes:
- When
isPressed is true:
translate-y-2: Moves the key down to create a pressed effect.
[box-shadow:0_0px_0_0_rgba(0,0,0,0)]: Removes the box shadow to enhance the pressed look.
border-b-0: Removes the bottom border.
bg-blue-600: Changes the background color to indicate an active state.
- When
isPressed is false:
[box-shadow:0_10px_0_0_#1b6ff8]: Adds a box shadow to create a 3D unpressed effect.
border-b border-blue-400: Adds a bottom border.
bg-blue-500: Sets the background color for the unpressed state.
-
Conditional Styles: The style attribute adjusts the font size based on the isPressed state.
For Pagination with Multiple Keys:
If you're planning to use this for pagination and need multiple keys where only one is active at a time, you can modify the code to handle multiple keys:
<script>
// Array of keys for pagination
let keys = ['A', 'B', 'C', 'D'];
// Variable to track the currently active key
let activeKey = null;
// Function to handle key press
function handlePress(key) {
activeKey = key; // Set the pressed key as active
}
</script>
<!-- Loop through the keys and render each one -->
{#each keys as key}
<kbd
on:click={() => handlePress(key)}
class="cursor-pointer select-none
transition-all duration-150
text-white font-bold text-lg
{activeKey === key
? 'translate-y-2 [box-shadow:0_0px_0_0_rgba(0,0,0,0)] border-b-0 bg-blue-600'
: '[box-shadow:0_10px_0_0_#1b6ff8] border-b border-blue-400 bg-blue-500'}"
style="{activeKey === key ? 'font-size: 20px' : 'font-size:18px'}">
{key}
</kbd>
{/each}
Explanation for Multiple Keys:
-
Dynamic Rendering: The {#each} block renders each key in the keys array.
-
Active State Management: The activeKey variable keeps track of which key is currently active. Clicking a key sets it as the active key.
-
Conditional Styling: The classes and styles are applied based on whether the key is the activeKey.
With this setup, clicking a key will give it the pressed appearance and keep it in that state until another key is clicked, perfect for pagination controls.
Note: Ensure that you have Tailwind CSS and any necessary plugins (like DaisyUI, if you're using it) properly configured in your project to utilize these classes.