question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow for booleans and numbers to be passed via data attributes

See original GitHub issue

What

Ensure that config passed by data attributes is typed sensibly for the way we want to use it.

Why

Attributes passed via data attributes will always be strings, but in some cases we want to be able to treat them as booleans or numbers.

For example:

  • data-prevent-double-click on buttons which is a boolean
  • data-maxlength on the character count which is a number

Who needs to work on this

Developers

Who needs to review this

Developers

Done when

  • Attributes that we expect to be booleans or numbers are typed as such when used as config in a component
  • We have good test coverage of any function(s) required to implement this

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
36degreescommented, Sep 12, 2022

We touched on it briefly in https://github.com/alphagov/govuk-frontend/issues/2736#issuecomment-1208303510 but I think the biggest downsides were:

  • It means that you need a way to JSON encode things from the ‘view’ layer (so that we can convert the Nunjucks options for i18n into the JSON object). We have this in Nunjucks via the dump filter but we weren’t confident that port maintainers would have this available in every templating language
  • It felt messier in terms of dealing with quotes and escaping

As flagged in the linked issue, Bootstrap now support this via data-bs-config but that’s in addition to the individual data attributes.

0reactions
colinrotherhamcommented, Sep 12, 2022

For options/config did the https://github.com/alphagov/govuk-frontend/issues/2736 JSON approach get ruled out?

Input: JSON string from data attribute

<div id="my-component" data-config='{ "points": 100, "default": true }'>
  <!-- Example -->
</div>

Output: JavaScript object with types

const component = document.getElementById('module')
const config = JSON.parse(component.dataset.config)

console.log(config) // { points: 100, default: true }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using boolean-value of attributes in JSX - Stack Overflow
You can just omit the value assignment; the attribute will be assigned a value of true by default. Instead of doing any of:...
Read more >
Boolean attributes - HTML 5
The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value....
Read more >
How You Can Use HTML5 Custom Data Attributes and Why
Gajendar Singh walks you through HTML5 custom data attributes, why they're useful, ... Let's say a given class name begins with numbers.
Read more >
How to Use HTML Data Attributes - DEV Community ‍ ‍
Adding a data attribute is easy. Any HTML element can have any number of data attributes added to its opening tag. We simply...
Read more >
Using data attributes - Learn web development | MDN
To get a data attribute through the dataset object, get the property by the part of the attribute name after data- (note that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found