ux uses localStorage
to remember features such as user preferences or display modes.
- feature can be any string
- features are either enabled, disabled, or unknown
ux.enabled()
: Get an array containing the names of all enabled featuresux.enabled(feature)
: Test if feature is enabled. Return boolean
ux.disabled()
: Get an array containing the names of all disabled featuresux.disabled(feature)
: Test if feature is disabled. Return boolean
ux.known()
: Get an array containing the names of all known featuresux.known(feature)
: Test if feature is known. Return boolean
- Enable feature, and trigger associated listeners
- Disable feature, and trigger associated listeners
- Toggle (either enable or disable) feature, and trigger associated listeners
- Forget feature, and trigger associated listeners
- Forget all features
Emitters have emitter methods
.on(feature, listener)
.off(feature?, listener?)
.once(feature, listener)
.emit(feature, ...args)
ux.enable.on(feature, listener)
ux.disable.on(feature, listener)
ux.forget.on(feature, listener)
ux.enable.off(feature?, listener?)
ux.disable.off(feature?, listener?)
ux.forget.off(feature?, listener?)
- etc.
// Listen for when 'crazycolors' is enabled
ux.enable.on('crazycolors', function(feature) {
document.querySelector('html').classList.add(feature)
console.log(feature + ' enabled')
})
// Enable 'crazycolors'
ux.enable('crazycolors')
You normally would want to also listen for when the same feature is disabled and do an opposing action. You're also likely to have multiple (and maybe related) features. ux
doesn't care what your features do. It only provides the API for enabling, disabling, and remembering them. Do what makes sense for your features and users :)