From 189895e8faa1e57e9c595e73b71f5b83f31d3811 Mon Sep 17 00:00:00 2001 From: John Cowen Date: Tue, 1 Feb 2022 16:39:02 +0000 Subject: [PATCH] ui: style-map helper (#12203) --- .../consul-ui/app/helpers/style-map.js | 22 +++++++ .../consul-ui/app/helpers/style-map.mdx | 58 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 ui/packages/consul-ui/app/helpers/style-map.js create mode 100644 ui/packages/consul-ui/app/helpers/style-map.mdx diff --git a/ui/packages/consul-ui/app/helpers/style-map.js b/ui/packages/consul-ui/app/helpers/style-map.js new file mode 100644 index 0000000000..259effd1e1 --- /dev/null +++ b/ui/packages/consul-ui/app/helpers/style-map.js @@ -0,0 +1,22 @@ +import { helper } from '@ember/component/helper'; + +/** + * Conditionally maps styles to a string ready for typical DOM + * usage (i.e. semi-colon delimited) + * + * @typedef {([string, (string | undefined), string] | [string, (string | undefined)])} styleInfo + * @param {styleInfo[]} entries - An array of `styleInfo`s to map + * @param {boolean} transform=true - whether to perform the build-time 'helper + * to modifier' transpilation. Note a transpiler needs installing separately. + */ +const styleMap = (entries, transform = true) => { + const str = entries.reduce((prev, [prop, value, unit = '']) => { + if (value == null) { + return prev; + } + return `${prev}${prop}:${value.toString()}${unit};`; + }, ''); + return str.length > 0 ? str : undefined; +}; + +export default helper(styleMap); diff --git a/ui/packages/consul-ui/app/helpers/style-map.mdx b/ui/packages/consul-ui/app/helpers/style-map.mdx new file mode 100644 index 0000000000..cb7fb6362b --- /dev/null +++ b/ui/packages/consul-ui/app/helpers/style-map.mdx @@ -0,0 +1,58 @@ +# style-map + +`{{style-map}}` is used to easily add a list of styles, conditionally, and +have them all formatted nicely to be printed in a DOM `style` attribute. + +As well as an entry-like array you can also pass an additional `unit` property +as the 3rd item in the array. This is to make it easier to do mathamatical +calculations for units without having to use `(concat)`. + +If any property has a value of `null` or `undefined`, that style property will +not be included in the resulting string. + +```hbs preview-template +
+
+ This div has the correct style added/omitted. +
+
+ + style={{style-map + (array 'outline' '1px solid red') + (array 'width' '600px') + (array 'height' 100 'px') + (array 'padding' 1 'rem') + (array 'background' null) + }} + +
+
+``` + +## Positional Arguments + +| Argument | Type | Default | Description | +| --- | --- | --- | --- | +| `entries` | `styleInfo[]` | | An array of `styleInfo`s to map | + +## Named Arguments + +| Argument | Type | Default | Description | +| --- | --- | --- | --- | +| `transform` | `boolean` | true | whether to perform the build-time 'helper to modifier' transpilation | + +## Types + +| Type | Default | Description | +| --- | --- | --- | +| `styleInfo` | `([string, (string \| undefined), string] \| [string, (string \| undefined)])` | | + +