- Docs
- Toggler
Action Toggler
This function provides more flexibility and can be used for various toggle scenarios beyond navigation.
The
actionToggler function creates a toggle mechanism that can change the attributes of multiple target elements when a trigger element is clicked. Purpose
- To create interactive UI elements that can toggle between two states
- To apply different attributes to target elements based on the toggle state
- To provide a callback function that runs on each toggle action
Usage
import { actionToggler } from '@flexilla/utilities';
actionToggler({
trigger: '#toggleButton',
targets: [
{
element: '#target1',
attributes: {
initial: { 'aria-hidden': 'true', class: 'hidden' },
to: { 'aria-hidden': 'false', class: 'visible' }
}
},
{
element: '#target2',
attributes: {
initial: { class: 'bg-red' },
to: { class: 'bg-blue' }
}
}
],
onToggle: ({ isExpanded }) => {
console.log('Toggle state:', isExpanded);
}
}); Behavior
- On initialization, it applies the
initialattributes to all target elements - When the trigger is clicked, it toggles between
initialandtoattributes for all targets - It updates the
aria-expandedattribute of the trigger element - It calls the
onTogglecallback (if provided) with the current toggle state
Parameters
The
actionToggler function accepts an options object with the following properties: -
trigger: A string selector or HTMLElement that triggers the toggle action when clicked -
targets: An array of target objects, each containing:-
element: A string selector or HTMLElement to be toggled -
attributes: An object withinitialandtostates, each containing attribute key-value pairs
-
-
onToggle: (Optional) A callback function that receives the current toggle state