Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/toggle-attribute/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,44 @@ import '@travelopia/web-components/dist/toggle-attribute/style.css';
<option value="">Select</option>
<option value="First">First</option>
<option value="Second">Second</option>
<option value="Third">Third</option>
<option value="Fourth">Fourth</option>
<option value="Fifth">Fifth</option>
<option value="All">All</option>
</select>
</tp-toggle-attribute>

<div class="toggle-target-variable" data-toggle-value="First,All">
Toggled First
</div>
<div class="toggle-target-variable" data-toggle-non-empty-value="yes">
<!-- add the data-toggle-non-empty-value="yes" attribute to toggle on with all non empty values. -->
This will be toggled for all non empty values.
</div>
<div class="toggle-target-variable" data-toggle-value="Second,All">
Toggled Second
</div>

<!-- Toggle for non empty values. -->
<p>Toggle with non empty values</p>

<!-- Select value -->
<tp-toggle-attribute target=".toggle-target-non-empty" non-empty-value="yes">
<select>
<option value="">Select</option>
<option value="First">First</option>
<option value="Second">Second</option>
<option value="Third">Third</option>
<option value="Fourth">Fourth</option>
<option value="Fifth">Fifth</option>
<option value="All">All</option>
</select>
</tp-toggle-attribute>

<div class="toggle-target-non-empty">
Toggle for all non empty values.
</div>

<p>Button with click event</p>
<tp-toggle-attribute event="click" target=".button-target">
<button>Toggle using class</button>
Expand All @@ -101,12 +128,13 @@ import '@travelopia/web-components/dist/toggle-attribute/style.css';

| Attribute | Required | Values | Notes |
|------------------------|----------|------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------|
| target | Yes | <selector or the target> | This is required if group is not mentioned |
| target | Yes | <selector or the target> | This is required if group is not mentioned |
| attribute | No | <attribute key> | The attribute to toggle. Default: `toggled` |
| attribute-value | No | <attribute value> | The attribute value when its. Default: `yes` |
| values | No | <comma separated values to match> | If this is specified, these comma separated values are matched with the value of the trigger. If they match, the target(s) is/are toggled. Same goes for having a `data-toggle-value` attribute on a target. |
| value | No | <comma separated values to match> | If this is specified, these comma separated values are matched with the value of the trigger. If they match, the target(s) is/are toggled. Same goes for having a `data-toggle-value` attribute on a target. |
| 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 |
| 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`. |
| non-empty-value | No | yes | A boolean attribute that signifies whether or not the targets should be toggled for all non empty values on the trigger.

## Events

Expand Down
27 changes: 27 additions & 0 deletions src/toggle-attribute/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,44 @@
<option value="">Select</option>
<option value="First">First</option>
<option value="Second">Second</option>
<option value="Third">Third</option>
<option value="Fourth">Fourth</option>
<option value="Fifth">Fifth</option>
<option value="All">All</option>
</select>
</tp-toggle-attribute>

<div class="toggle-target-variable" data-toggle-value="First,All">
Toggled First
</div>
<div class="toggle-target-variable" data-toggle-non-empty-value="yes">
<!-- add the data-toggle-non-empty-value="yes" attribute to toggle on with all non empty values. -->
This will be toggled for all non empty values.
</div>
<div class="toggle-target-variable" data-toggle-value="Second,All">
Toggled Second
</div>

<!-- Toggle for non empty values. -->
<p>Toggle with non empty values</p>

<!-- Select value -->
<tp-toggle-attribute target=".toggle-target-non-empty" non-empty-value="yes">
<select>
<option value="">Select</option>
<option value="First">First</option>
<option value="Second">Second</option>
<option value="Third">Third</option>
<option value="Fourth">Fourth</option>
<option value="Fifth">Fifth</option>
<option value="All">All</option>
</select>
</tp-toggle-attribute>

<div class="toggle-target-non-empty">
Toggle for all non empty values.
</div>

<p>Button with click event</p>
<tp-toggle-attribute event="click" target=".button-target">
<button>Toggle using class</button>
Expand Down
54 changes: 40 additions & 14 deletions src/toggle-attribute/tp-toggle-attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class TPToggleAttributeElement extends HTMLElement {
update(): void {
// Get trigger elements.
const triggerSelector: string = this.getAttribute( 'trigger' ) ?? ':scope > *';
const triggers: NodeListOf<HTMLElement> | null = this.querySelectorAll( triggerSelector );
const triggers: NodeListOf<HTMLElement> = this.querySelectorAll( triggerSelector );

// Exit the function if no triggers are found.
if ( ! triggers ) {
Expand Down Expand Up @@ -61,7 +61,7 @@ export class TPToggleAttributeElement extends HTMLElement {
// Check if trigger has a value, example: form inputs.
if ( value || '' === value ) {
// Check if we have a value.
if ( this.hasAttribute( 'value' ) ) {
if ( this.hasAttribute( 'value' ) || this.hasAttribute( 'non-empty-value' ) ) {
this.toggleByValueAttribute( value );
} else {
this.toggleByTargetDataValue( value );
Expand All @@ -78,22 +78,38 @@ export class TPToggleAttributeElement extends HTMLElement {
* @param {string} value Trigger's value.
*/
toggleByValueAttribute( value: string = '' ): void {
// Get value to listen for.
const values: string[] = ( this.getAttribute( 'value' ) ?? '' ).split( ',' );

// Get the target elements.
const targetElements = this.getTargetElements();

// Check if we can continue
if ( ! ( values.length && targetElements ) ) {
if ( ! targetElements ) {
// We can't.
return;
}

// Initialize values.
let values: string[] = [];

// Get value to listen for.
const valuesAttribute = this.getAttribute( 'value' );
const nonEmptyValuesAttribute = this.hasAttribute( 'non-empty-value' );

// Can we proceed?
if ( ! valuesAttribute && ! nonEmptyValuesAttribute ) {
// Nope.
return;
}

// Do we have the values attribute?
if ( valuesAttribute ) {
// Yes, split it.
values = valuesAttribute.split( ',' );
}

// Toggle the target elements.
targetElements.forEach( ( target ) => {
// Toggle the target's attribute if the target and trigger have the same value.
if ( values.includes( value ) ) {
if ( ( values.length && values.includes( value ) ) || ( nonEmptyValuesAttribute && value ) ) {
this.toggleTargetAttribute( target, 'on' );
} else {
this.toggleTargetAttribute( target, 'off' );
Expand All @@ -118,17 +134,27 @@ export class TPToggleAttributeElement extends HTMLElement {

// Toggle the target elements.
targetElements.forEach( ( target: HTMLElement ): void => {
// Get values.
const values: string[] = ( target.getAttribute( 'data-toggle-value' ) ?? '' ).split( ',' );
// Get values and split them. Set an empty array otherwise.
const valuesAttribute = target.getAttribute( 'data-toggle-value' );
const nonEmptyValuesAttribute = target.hasAttribute( 'data-toggle-non-empty-value' );

// Check if we can continue
if ( ! values.length ) {
// We can't.
// Can we proceed?
if ( ! valuesAttribute && ! nonEmptyValuesAttribute ) {
// Nope, bail.
return;
}

// Toggle on element attribute if it matches value.
if ( values.includes( value ) ) {
// Initialize values.
let values: string[] = [];

// Null check.
if ( valuesAttribute ) {
// Assign the values.
values = valuesAttribute.split( ',' );
}

// Toggle on element attribute if it matches value or it does not have a data-toggle-value attribute in which case it will match with all non empty values.
if ( ( values.length && values.includes( value ) ) || ( nonEmptyValuesAttribute && value ) ) {
this.toggleTargetAttribute( target, 'on' );
} else {
this.toggleTargetAttribute( target, 'off' );
Expand Down
Loading