Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                

Swap

Simple element classname swapping.


Demos

Basic

Swap classes on a target element.

<a href="#swap_1" class="handle js-swap"
  data-swap-target="#swap_1"
>
  Handle Element
</a>

<div id="swap_1" class="target">
  Target Element
</div>
import { Swap, Utils } from 'formstone';

Utils.ready(() => {
  Swap.construct('.js-swap');
});
.handle,
.target {
  border: var(--border-size) solid var(--color-black);
  border-radius: var(--border-size);
  color: var(--color-black);
  display: block;
  margin: 20px 10vw;
  padding: 20px;
  text-align: center;
  text-decoration: none;
  transition: all 0.15s linear;
  min-width: 60%;
}

.handle {
  background: var(--color-blue);
}

.handle.fs-swap-active {
  background: var(--color-yellow);
}

.target {
  background: var(--color-gray-20);
}

.target.fs-swap-active {
  background: var(--color-gray-10);
}

.fs-swap-enabled::after {
  content: ' (Enabled)';
}

.fs-swap-active::after {
  content: ' (Active)';
}

Accordion

Swap classes on a group of elements. Use <button> handles and set aria so each handle exposes aria-controls and aria-expanded (see Accessibility).

<button type="button" class="handle js-swap"
  data-swap-target="#swap_1"
  data-swap-group="#group_1"
>
  Handle Element 1
</button>

<div id="swap_1" class="target">
  Target Element 1
</div>

<button type="button" class="handle js-swap"
  data-swap-target="#swap_2"
  data-swap-group="#group_1"
>
  Handle Element 2
</button>

<div id="swap_2" class="target">
  Target Element 2
</div>

<button type="button" class="handle js-swap"
  data-swap-target="#swap_3"
  data-swap-group="#group_1"
>
  Handle Element 3
</button>

<div id="swap_3" class="target">
  Target Element 3
</div>
import { Swap, Utils } from 'formstone';

Utils.ready(() => {
  Swap.construct('.js-swap', {
    aria: true,
    collapse: true,
  });
});
.handle,
.target {
  border: var(--border-size) solid var(--color-black);
  border-radius: var(--border-size);
  color: var(--color-black);
  display: block;
  margin: 20px 10vw;
  padding: 20px;
  text-align: center;
  text-decoration: none;
  transition: all 0.15s linear;
  min-width: 60%;
}

.handle {
  background: var(--color-blue);
  cursor: pointer;
  font: inherit;
  width: calc(100% - 20vw);
}

.handle.fs-swap-active {
  background: var(--color-yellow);
}

.target {
  background: var(--color-gray-10);
}

.target.fs-swap-enabled {
  display: none;
}

.target.fs-swap-active {
  display: block;
}

.fs-swap-enabled::after {
  content: ' (Enabled)';
}

.fs-swap-active::after {
  content: ' (Active)';
}

Linked

Swap classes on a set of elements.

<a href="#swap_1" class="handle js-swap"
  data-swap-target="#swap_1"
  data-swap-linked="#set_1"
  data-swap-options='{
  "collapse": true
}'
>
  Handle Element
</a>

<a href="#swap_1" class="handle action js-swap"
  data-swap-target="#swap_1"
  data-swap-linked="#set_1"
  data-swap-options='{
  "collapse": true
}'
>
  Handle Element
</a>

<div id="swap_1" class="target">
  Target Element
</div>
import { Swap, Utils } from 'formstone';

Utils.ready(() => {
  Swap.construct('.js-swap');
});
.handle,
.target {
  border: var(--border-size) solid var(--color-black);
  border-radius: var(--border-size);
  color: var(--color-black);
  display: block;
  margin: 20px 10vw;
  padding: 20px;
  text-align: center;
  text-decoration: none;
  transition: all 0.15s linear;
  min-width: 60%;
}

.handle.action {
  position: absolute;
  bottom: 20px;
  right: 20px;
  z-index: 1;
  margin: 0;
  min-width: unset;
}

.handle {
  background: var(--color-blue);
}

.handle.fs-swap-active {
  background: var(--color-yellow);
}

.target {
  background: var(--color-gray-20);
}

.target.fs-swap-active {
  background: var(--color-gray-10);
}

.fs-swap-enabled::after {
  content: ' (Enabled)';
}

.fs-swap-active::after {
  content: ' (Active)';
}

Responsive

Set maxWidth (or minWidth) to enable Swap only at some viewport widths. At 600px and narrower these sections collapse into an accordion; wider, Swap is disabled, every section shows, and the swap:enable and swap:disable events turn the buttons on and off. Resize the preview, or open it in CodePen, to see it switch.

<h2 class="heading">
  <button type="button" class="handle js-swap" disabled
    data-swap-target="#section_1"
    data-swap-group="#sections"
  >
    Section One
  </button>
</h2>
<div id="section_1" class="target">
  Section One content.
</div>
<h2 class="heading">
  <button type="button" class="handle js-swap" disabled
    data-swap-target="#section_2"
    data-swap-group="#sections"
  >
    Section Two
  </button>
</h2>
<div id="section_2" class="target">
  Section Two content.
</div>
<h2 class="heading">
  <button type="button" class="handle js-swap" disabled
    data-swap-target="#section_3"
    data-swap-group="#sections"
  >
    Section Three
  </button>
</h2>
<div id="section_3" class="target">
  Section Three content.
</div>
import { Swap, Utils } from 'formstone';

Utils.ready(() => {
  let handles = Utils.select('.js-swap');

  // The buttons only do something while Swap is enabled
  Utils.on(handles, 'swap:enable', (e) => {
    e.currentTarget.disabled = false;
  });

  Utils.on(handles, 'swap:disable', (e) => {
    e.currentTarget.disabled = true;
  });

  Swap.construct('.js-swap', {
    aria: true,
    collapse: true,
    maxWidth: '600px',
  });
});
body {
  padding: 20px 10vw;
}

.heading {
  font-size: inherit;
  margin: 0;
}

.handle {
  background: var(--color-blue);
  border: var(--border-size) solid var(--color-black);
  border-radius: var(--border-size);
  color: var(--color-black);
  cursor: pointer;
  display: block;
  font: inherit;
  font-weight: bold;
  margin: 20px 0 0;
  padding: 20px;
  text-align: left;
  width: 100%;
}

.handle.fs-swap-active {
  background: var(--color-yellow);
}

.handle.fs-swap-enabled::after {
  content: ' +' / '';
}

.handle.fs-swap-active::after {
  content: ' −' / '';
}

/* Wider than 600px: a plain heading */
.handle:disabled {
  background: none;
  border-color: transparent;
  cursor: default;
  padding: 0;
}

.target {
  background: var(--color-gray-10);
  border: var(--border-size) solid var(--color-black);
  border-radius: var(--border-size);
  margin: 10px 0 0;
  padding: 20px;
}

.target.fs-swap-enabled {
  display: none;
}

.target.fs-swap-active {
  display: block;
}

Options

Set instance options by passing a valid object at initialization, or to the public .defaults() method. Custom options for a specific instance can also be set by attaching a data-swap-options attribute containing a properly formatted JSON object to the target element.

Name Type Default Description
aria boolean false Set aria-controls and aria-expanded on the handle while enabled (see Accessibility)
classes object ... State classes applied by the instance
classes.enabled string 'fs-swap-enabled' Class applied when instance is enabled
classes.active string 'fs-swap-active' Class applied when instance is active
classes.inactive string 'fs-swap-inactive' Class applied when instance is inactive
collapse boolean true Allow instance to collapse
maxWidth string | number Infinity Maximum viewport width to enable instance
minWidth string '0px' Width to auto-enable instance

Data attributes can be used for instance setup.

Name Type Description
data-swap-options string (JSON) JSON encoded object containing instance options
data-swap-target string Selector for element(s) that should share swap state classes
data-swap-group string Group key for mutually exclusive swap behavior
data-swap-linked string Link key for synchronized swap instances
data-swap-active string Marks an instance as active by default when enabled

Methods

Methods are publicly available to all active instances, unless otherwise stated.

Name Description
.activate() Activates instance
.construct() Initializes Swap on target elements
.deactivate() Deactivates instance
.defaults() Sets default options for future initialization
.destroy() Removes instance and all related data
.disable() Disables instance
.enable() Enables instance

.activate()

Activates instance.

Parameters

Name Type Default Description
internal boolean (optional) false Internal flag to prevent linked recursion

Examples

el.Swap.activate();

.construct()

Initializes Swap on target elements.

Returns

Type Description
NodeList NodeList of target elements

Parameters

Name Type Default Description
selector string (required) '' Selector string to target
options object (optional) {} Object containing instance options

Examples

let targets = Swap.construct('.js-swap');
Swap.construct('.js-swap', { collapse: false });

.deactivate()

Deactivates instance.

Parameters

Name Type Default Description
internal boolean (optional) false Internal flag to prevent linked recursion

Examples

el.Swap.deactivate();

.defaults()

Sets default options for future initialization.

Parameters

Name Type Default Description
options object (required) {} Object containing default options

Examples

Swap.defaults({
  collapse: false
});

.destroy()

Removes instance and all related data.

Examples

el.Swap.destroy();

.disable()

Disables instance.

Parameters

Name Type Default Description
internal boolean (optional) false Internal flag to prevent linked recursion

Examples

el.Swap.disable();

.enable()

Enables instance.

Parameters

Name Type Default Description
internal boolean (optional) false Internal flag to prevent linked recursion

Examples

el.Swap.enable();

Events

Events are triggered on the target instance's element, unless otherwise stated.

Name Description
swap:activate Instance activated
swap:deactivate Instance deactivated, including when another member of its data-swap-group activates (fired before that member's swap:activate)
swap:enable Instance enabled
swap:disable Instance disabled

Styles

Classes allow deeper customization and indicate the current state of a specific instance.

Classname Type Description
.fs-swap-enabled modifier Indicates enabled state
.fs-swap-active modifier Indicates active state
.fs-swap-inactive modifier Indicates inactive state

Accessibility

Swap only swaps classes by default. For disclosures (accordions, show/hide toggles), set aria: true. While the instance is enabled, the handle gets aria-controls pointing at its data-swap-target element(s) and an aria-expanded that follows the active state, including group and linked changes. Targets without an id get a generated one. When the instance is disabled (by minWidth/maxWidth or disable()) or destroyed, the generated ids are removed and the handle's original aria-controls and aria-expanded are restored.

Use a <button type="button"> as the handle, so it's keyboard and screen reader operable. With aria: true, other handles log a console warning; Swap doesn't add roles or key handling. For a heading, put the button inside it.