0.0.4 • Published 5 years ago

@fiveid/scenes v0.0.4

Weekly downloads
4
License
MIT
Repository
-
Last release
5 years ago

Scenes

Minimalist scene-switcher module in vanilla JS.

Basic usage

Scenes toggles the active classname of elements. In the simple example below, clicking the "Next" or "Previous" buttons will switch visibility between the two scenes.

  • index.html
<div class="scene active">
  <p>This is scene 1</p>
  <button class="scene__next">Next</button>
</div>
<div class="scene">
  <p>This is scene 2</p>
  <button class="scene__prev">Previous</button>
</div>
  • style.css
.scene {
  display: none;
}

.scene {
  display: block;
}
  • main.js
import { Scenes } from "@fiveid/scenes";

const scenes = new Scenes();

Options

Customise Scenes behaviour by passing an options object on initialisation.

Option keyDescription
selectorsAn object containing the DOM selectors that trigger certain actions (see below)
activeClassThe classname that designates a scene element as "active" (default: active)
dataKeyThe HTML data attribute used to identify which scene index to set as active, in JS format (default: sceneIndex)
initialIndexThe index of the active scene on load (default: 0)
preTransitionA function that is called before changing active state. This can be used to alter the next active scene (see full example below)
postTransitionA function that is called after changing active state (see full example below)

Selectors

Override default DOM selectors in options.

State changes are triggered when users click on DOM elements. The selectors that trigger these actions can be configured (or you can keep the default values).

KeyDescriptionDefault
sceneThe element that will be set as active or not.scene
nextSets the subsequent adjacent scene element to active.scene__next
prevSets the previous adjacent scene element to active.scene_prev
resetSets the scene element at the initial index to active.scene__reset
gotoGoes to a scene element at an index specified by a HTML data attribute (e.g. data-scene-index).scene__goto
popSets the previously active scene element as active.scene__pop

Example custom selectors:

const mySelectors = {
  scene: ".block",
  next: ".block__next",
  prev: ".block__prev",
  reset: ".block__reset",
  goto: ".block__goto",
  pop: ".block__pop"
};

Pre-transition

You can trigger a side effect or alter the typical state change behaviour by passing a function to the preTransition option.

The function has three parameters:

  • state: an object containing currentIndex and nextIndex
  • callback: a function that needs to be called to trigger the state change. You can optionally pass a nextIndex value to the callback to determine which scene will next be set as active
  • event: if the transition was triggered by a JS event, the event object will be passed to the preTransition function

Example:

const preTransition = (state, callback, event) => {
  if (
    state.nextIndex === RESTRICTED_SCENE &&
    userIsAllowedRestrictedScene === false
  ) {
    return callback(state.currentIndex);
  }

  callback();
};

Post-transition

You can trigger a side effect after changing active state by passing a function to the postTransition option.

The function has two parameters:

  • state: an object containing currentIndex and prevIndex
  • event: if the transition was triggered by a JS event, the event object will be passed to the preTransition function

Example:

const postTransition = (state, event) => {
  lazyLoadContentOfActiveScene(state.currentIndex);
  disableMediaContentOfPreviousScene(state.prevIndex);
};

Instance properties and methods

Some potentially useful instance methods and properties are exposed.

activeIndex

The index of the currently active scene.

Returns:

  • : number

transitionTo

A function that sets the scene at index as active. (Note: calling transitionTo will still trigger the preTransition and postTransition functions).

Parameters:

  • index: number - the target scene index (default: 0)

Example:

const scenes = new Scenes();

fetchDataFromSomewhere().then(done => {
  if (done.ok) {
    scenes.transitionTo(1); // success scene
  } else {
    scenes.transitionTo(2); // error scene
  }
});
0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago