The toggleState
utility is a lightweight helper for managing boolean state in your components. It encapsulates a tracked property (current
) and a toggle
method that can be used to update the state. This utility supports:
toggle()
without arguments simply inverts the current state.toggle(true)
or toggle(false)
to explicitly set the state.checked
property.import { toggleState } from '@frontile/utilities';
You can create a toggle state using the helper function:
import { toggleState } from '@frontile/utilities';
let state = toggleState(false, (val) => {
console.log('State changed to:', val);
});
state.toggle(); // Flips the current state.
state.toggle(true); // Sets the state to true.
// If used as an event handler, it reads the `checked` value:
<template><input type='checkbox' {{on 'change' state.toggle}} /></template>
Below is an example of a component that uses toggleState:
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { toggleState } from '@frontile/utilities';
import { Button } from '@frontile/buttons';
import { on } from '@ember/modifier';
export default class MyTestComponent extends Component {
// Create a toggle state with an initial value of false.
showingState = toggleState(false);
// An explicit method to set the state to true.
show = () => {
this.showingState.toggle(true);
};
<template>
<Button
data-test-id='button-toggle'
{{on 'click' this.showingState.toggle}}
>
Toggle
</Button>
<Button data-test-id='button-direct' {{on 'click' this.show}}>
Show (explicit value)
</Button>
<input
data-test-id='input-toggle'
type='checkbox'
checked={{this.showingState.current}}
{{on 'change' this.showingState.toggle}}
/>
{{#if this.showingState.current}}
<div data-test-content>
Hello World
</div>
{{/if}}
</template>
}
You can also use toggleState directly within your templates:
import { toggleState } from '@frontile/utilities';
import { Button } from '@frontile/buttons';
import { on } from '@ember/modifier';
<template>
{{#let (toggleState) as |state|}}
<Button data-test-id='button-toggle' {{on 'click' state.toggle}}>
Toggle
</Button>
{{#if state.current}}
<div data-test-content>
Hello World
</div>
{{/if}}
{{/let}}
</template>
Below is an example that demonstrates how to use toggleState
with an onChange
callback. The callback is invoked whenever the toggle state changes, allowing you to perform side effects such as logging or updating external state.
import Component from '@glimmer/component';
import { toggleState } from '@frontile/utilities';
import { Button } from '@frontile/buttons';
import { on } from '@ember/modifier';
export default class CallbackToggleExample extends Component {
// Create a toggle state with an initial value of false and an onChange callback.
toggle = toggleState(false, (newValue: boolean) => {
console.log('Toggle state changed to:', newValue);
});
<template>
{{#if this.toggle.current}}
<div data-test-content>
The toggle is ON
</div>
{{else}}
<div data-test-content>
The toggle is OFF
</div>
{{/if}}
<Button data-test-id='button-toggle' {{on 'click' this.toggle.toggle}}>
Toggle State
</Button>
</template>
}