Skip to content

Commit

Permalink
Use clsx to chain class name attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrHovhannisyan committed Dec 7, 2022
1 parent 641cea6 commit fbf90d6
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 7 deletions.
1 change: 1 addition & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = (eleventyConfig) => {
</html>`,
iframeAttributes: {
height: '100',
class: 'code-demo',
},
});

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ button.addEventListener('click', () => {
{% endcodeDemo %}
````

If you're using Nunjucks, you'll need to use [the `safe` filter](https://mozilla.github.io/nunjucks/templating.html#autoescaping) to opt out of auto-escaping the HTML.

### Setting HTML Attributes on the Code Demo

As we saw, you can set HTML attributes globally on all code demos in your `.eleventy.js` config using the `iframeAttributes` option, but you can also pass in attributes on a case-by-case basis. The example below leverages Nunjucks's support for [keyword arguments](https://mozilla.github.io/nunjucks/templating.html#keyword-arguments) to create a code demo that is 400 pixels tall:
Expand Down
26 changes: 21 additions & 5 deletions demo/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
permalink: /
---

Below is an interactive code demo:

{% codeDemo 'Code demo', height="400" %}
{% set html %}
```html
<div class="buttons">
<button aria-label="Increment" data-step="1">&plus;</button>
<button aria-label="Decrement" data-step="-1">&minus;</button>
</div>
<output>0</output>
```
{% endset %}

{% set css %}
```css
* {
box-sizing: border-box;
Expand All @@ -38,7 +38,9 @@ button {
line-height: 1;
}
```
{% endset %}

{% set js %}
```js
const buttons = document.querySelectorAll('[data-step]');
const output = document.querySelector('output');
Expand All @@ -48,6 +50,20 @@ buttons.forEach((button) => {
count += Number(button.getAttribute('data-step'));
output.innerHTML = count;
});
});;
});
```
{% endcodeDemo %}
{% endset %}

Below is an interactive code demo:

{% codeDemo 'Code demo', height="400", class="another-class" %}
{{ html | safe }}
{{ css | safe }}
{{ js | safe }}
{% endcodeDemo %}

Code blocks used:

{{ html | safe }}
{{ css | safe }}
{{ js | safe }}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
},
"dependencies": {
"@minify-html/node": "^0.10.3",
"clsx": "^1.2.1",
"lodash.escape": "^4.0.1",
"markdown-it": "^13.0.1",
"outdent": "^0.8.0"
Expand Down
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ const minifyHtml = require('@minify-html/node');
const markdownIt = require('markdown-it');
const outdent = require('outdent');
const { parseCode, stringifyAttributes } = require('./utils');
const clsx = require('clsx');

/**
* Higher-order function that takes user configuration options and returns the plugin shortcode.
* @param {import('./typedefs').EleventyPluginCodeDemoOptions} options
*/
const makeCodeDemoShortcode = (options) => {
const sharedIframeAttributes = options.iframeAttributes;

/**
* @param {string} source The children of this shortcode, as Markdown code blocks.
* @param {string} title The title to set on the iframe.
Expand All @@ -23,7 +26,9 @@ const makeCodeDemoShortcode = (options) => {
const css = parseCode(tokens, 'css');
const html = parseCode(tokens, 'html');
const js = parseCode(tokens, 'js');
const iframeAttributes = stringifyAttributes({ ...options.iframeAttributes, ...props });

const className = clsx(sharedIframeAttributes?.class, props.class);
const iframeAttributes = stringifyAttributes({ ...sharedIframeAttributes, ...props, class: className });

let srcdoc = `<!DOCTYPE html>${options.renderDocument({ html, css, js })}`;

Expand Down
2 changes: 1 addition & 1 deletion src/typedefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @typedef EleventyPluginCodeDemoOptions
* @property {string} name The shortcode name to use.
* @property {(args: RenderArgs) => string} renderDocument A render function to render the iframe's document definition.
* @property {Record<string, unknown>} iframeAttributes Any HTML attributes you want to set on the `<iframe>`.
* @property {Record<string, unknown>} [iframeAttributes] Any HTML attributes you want to set on the `<iframe>` (optional).
*/

module.exports = {};
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,11 @@ cliui@^8.0.1:
strip-ansi "^6.0.1"
wrap-ansi "^7.0.0"

clsx@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/clsx/-/clsx-1.2.1.tgz#0ddc4a20a549b59c93a4116bb26f5294ca17dc12"
integrity sha512-EcR6r5a8bj6pu3ycsa/E/cKVGuTgZJZdsyUYHOksG/UHIiKfjxzRxYJpyVBwYaQeOvghal9fcc4PidlgzugAQg==

color-convert@^1.9.0:
version "1.9.3"
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.3.tgz#bb71850690e1f136567de629d2d5471deda4c1e8"
Expand Down

0 comments on commit fbf90d6

Please sign in to comment.