|
| 1 | +# Toggle Attribute |
| 2 | + |
| 3 | +<table width="100%"> |
| 4 | + <tr> |
| 5 | + <td align="left" width="70%"> |
| 6 | + <p>Built by the super talented team at <strong><a href="https://www.travelopia.com/work-with-us/">Travelopia</a></strong>.</p> |
| 7 | + </td> |
| 8 | + <td align="center" width="30%"> |
| 9 | + <img src="https://www.travelopia.com/wp-content/themes/travelopia/assets/svg/logo-travelopia-circle.svg" width="50" /> |
| 10 | + </td> |
| 11 | + </tr> |
| 12 | +</table> |
| 13 | + |
| 14 | +## Sample Usage |
| 15 | + |
| 16 | +This is a versatile component to turn an attribute on a target component on and off based on changes on a trigger. This is useful to hide or show things, or just change a state on a target. |
| 17 | + |
| 18 | +Example: |
| 19 | + |
| 20 | +```js |
| 21 | +// Import the component as needed: |
| 22 | +import '@travelopia/web-components/dist/toggle-attribute'; |
| 23 | +import '@travelopia/web-components/dist/toggle-attribute/style.css'; |
| 24 | +``` |
| 25 | + |
| 26 | +```html |
| 27 | +<!-- Target based on an explicit value --> |
| 28 | +<tp-toggle-attribute target="#toggle-target" value="Yes"> |
| 29 | + <select> |
| 30 | + <option value="">Select</option> |
| 31 | + <option value="Yes">Yes</option> |
| 32 | + <option value="No">No</option> |
| 33 | + </select> |
| 34 | +</tp-toggle-attribute> |
| 35 | + |
| 36 | +<!-- Target based on value of the trigger --> |
| 37 | +<tp-toggle-attribute target="#toggle-target"> |
| 38 | + <select> |
| 39 | + <option value="">Select</option> |
| 40 | + <option value="Yes">Yes</option> |
| 41 | + <option value="No">No</option> |
| 42 | + </select> |
| 43 | +</tp-toggle-attribute> |
| 44 | + |
| 45 | +<!-- Toggle based on a value of the trigger, but toggle other members of the same group off --> |
| 46 | +<tp-toggle-attribute group="my-group"> |
| 47 | + <select> |
| 48 | + <option value="">Select</option> |
| 49 | + <option value="Yes">Yes</option> |
| 50 | + <option value="No">No</option> |
| 51 | + </select> |
| 52 | +</tp-toggle-attribute> |
| 53 | + |
| 54 | +<!-- Example target --> |
| 55 | +<div id="toggle-target" data-toggle-value="Yes" data-toggle-group="my-group"> |
| 56 | + Yes |
| 57 | +</div> |
| 58 | +``` |
| 59 | + |
| 60 | +## Attributes |
| 61 | + |
| 62 | +| Attribute | Required | Values | Notes | |
| 63 | +|------------------------|----------|------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------| |
| 64 | +| target | Maybe | <selector or the target> | This is required if group is not mentioned | |
| 65 | +| group | Maybe | <name of the group> | The group name. Will be looking for the `data-toggle-group` attribute on targets | |
| 66 | +| attribute | No | <attribute key> | The attribute to toggle. Default: `toggled` | |
| 67 | +| attribute-value | No | <attribute value> | The attribute value when its. Default: `yes` | |
| 68 | +| value | No | <value to match> | If this is specified, this value is matched with the value of the trigger. If they match, the target is toggled | |
| 69 | +| trigger | No | <selector of the trigger> | If this is not specified, the direct child is treated as the trigger. If it is mentioned, it looks for this selector within the context | |
| 70 | +| closest-ancestor | No | <selector of the closest ancestor> | Default: `body`. If this is specified, the target is searched for within this selector, not on `body`. | |
| 71 | + |
| 72 | +## Events |
| 73 | + |
| 74 | +| Event | Notes | |
| 75 | +|-------------|------------------------------------------------| |
| 76 | +| triggered | When the trigger is fired, before any toggling | |
| 77 | +| toggled | When the target is toggled | |
| 78 | +| toggled-on | When the target is toggled on | |
| 79 | +| toggled-off | When the target is toggled off | |
0 commit comments