Skip to content

Commit fcfdb84

Browse files
committed
maint(pat-toggle): Add alias attribute for attr to toggle an attribute.
1 parent 910e3e9 commit fcfdb84

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/pat/toggle/documentation.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ For instance to show or hide a sidebar with a CSS class on the body tag.
88
The _toggle_ pattern can be used to toggle attribute values for objects.
99
It is most commonly used to toggle a CSS class.
1010

11-
<a href="#" class="pat-toggle" data-pat-toggle="selector: #work; attr: class; value: active">Start working</a>
11+
<a href="#"
12+
class="pat-toggle"
13+
data-pat-toggle="
14+
selector: #work;
15+
attribute: class;
16+
value: active">Start working</a>
1217
<div id="work">
1318
Working…
1419
</div>
@@ -67,3 +72,18 @@ The possible values for the `store` parameter are:
6772
- `none`: do not remember the toggle state (default).
6873
- `local`: remember the state as part of the local storage.
6974
- `session`: remember the status as part of the session storage.
75+
76+
77+
### Options reference
78+
79+
You can customise the behaviour of a switches through options in the
80+
`data-pat-toggle` attribute.
81+
82+
| Property | Default value | Values | Description | Type |
83+
| ---------- | ------------- | ------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------ |
84+
| `selector` | | | CSS selector matching elements where a class or an attribute should be toggled. | String |
85+
| `value` |   |   | One or more space seperated CSS class names to toggle on the element. Can only be used with `attr` set to class, which is the default. | String |
86+
| `event` | `click` | | A JavaScript event which triggers the toggler. The default is to listen to `click` events. | String |
87+
| `attr` | `class` | | The attribute which should be toggled. In case of the default `class`, class names are added or removed. In case of any other attribute the attribute as added or removed. | String |
88+
| `store` | `none` | `none` `session` `local` | How to store the state of a toggle. `none` does not remember the toggle state, `local` stores the state as part of the local storage and `session` stores the status as part of the session storage. | Mutually exclusive |
89+

src/pat/toggle/toggle.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ parser.addArgument("attr", "class");
1919
parser.addArgument("value");
2020
parser.addArgument("store", "none", ["none", "session", "local"]);
2121

22+
parser.addAlias("attribute", "attr");
23+
2224
export function ClassToggler(values) {
2325
this.values = values.slice(0);
2426
if (this.values.length > 1) this.values.push(values[0]);

src/pat/toggle/toggle.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,16 @@ describe("Pattern implementation", function () {
175175
store: "none",
176176
});
177177
});
178+
179+
it("1.6 - Accept attribute as an alias to attr.", function () {
180+
const instance = new Pattern(document.createElement("div"));
181+
const validated = instance._validateOptions([
182+
{ attribute: "disabled", selector: "input" },
183+
]);
184+
expect(validated.length).toEqual(1);
185+
expect(validated[0].attribute).toEqual("disabled");
186+
expect(validated[0].selector).toEqual("input");
187+
});
178188
});
179189

180190
describe("2 - When clicking on a toggle", function () {
@@ -226,6 +236,18 @@ describe("Pattern implementation", function () {
226236
await utils.timeout(1);
227237
expect(victims[0].disabled).toBe(false);
228238
});
239+
240+
it("2.3 - attributes are updated - use the alias", async function () {
241+
var $trigger = $(trigger);
242+
trigger.dataset.patToggle = ".victim; attribute: disabled";
243+
new Pattern($trigger);
244+
$trigger.click();
245+
await utils.timeout(1);
246+
expect(victims[0].disabled).toBe(true);
247+
$trigger.click();
248+
await utils.timeout(1);
249+
expect(victims[0].disabled).toBe(false);
250+
});
229251
});
230252

231253
describe("3 - Toggle event triggers", function () {

0 commit comments

Comments
 (0)