From 2185408fea9c806338db99410f85db746802ebcd Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Mon, 22 Oct 2018 19:20:20 +0200 Subject: [PATCH] css: support special css property `class` for adding custom classes to widgets (#361) --- CHANGELOG.md | 3 ++- resources/docs/extras/css-tips.md | 8 +++++++ src/browser/js/app/widgets/common/widget.js | 23 ++++++++++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 442b9927..b085dfe6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # Changelog -## 0.38.3 +## 0.38.4 - bug fixes - `@{parent}` calls not working consistently in clone widgets @@ -10,6 +10,7 @@ - undo/redo errors - widgets - 1-level nested property inheritance calls (`@{id_@{id_2}}`) are now updated dynamically + - support special css property `class` for adding custom classes to widgets - editor - major performance improvements (containers' children are reused whenever possible instead of being recreated) - removed widget deletion confirmation popup diff --git a/resources/docs/extras/css-tips.md b/resources/docs/extras/css-tips.md index e461fd03..f77333fc 100644 --- a/resources/docs/extras/css-tips.md +++ b/resources/docs/extras/css-tips.md @@ -34,6 +34,14 @@ CSS selectors can be used to apply styles to specific elements: !!! tip "" Mixing Inline and Selector syntaxes doesn't work, once you use selectors, you have to use the `:host` selector to target the widget element. +## Extra css classes: `class` + +This non-standard css property can be used to add custom css classes to the widget element: `class: my-custom-class;` +Multiple classes can be added (one per `class` statement). + +!!! warning "" + Using class names that are already used in the app can be hazardous. In order to avoid that, custom class names should be prefixed with something uncommon and preferably cool, such as `xxx-myclass` or `crispy-bacon-myclass`. + ## Layering: `z-index` Z-Axis ordering can be set using the `z-index` rule. Absolutely positionned widgets (when `top` or `left` is different from `auto`) have `z-index:10;` by default. diff --git a/src/browser/js/app/widgets/common/widget.js b/src/browser/js/app/widgets/common/widget.js index 0d441576..347f3ac4 100644 --- a/src/browser/js/app/widgets/common/widget.js +++ b/src/browser/js/app/widgets/common/widget.js @@ -767,7 +767,15 @@ class Widget extends EventEmitter { var css = String(this.getProp('css')), prefix = '#' + this.hash, scopedCss = scopeCss(css, prefix), - unScopedCss = '' + unScopedCss = '', + extraCssClasses = [] + + + // extra css "class" property + css = css.replace(/^[^\S\n]*class[^\S\n]*:[^\S\n]*([^;\n\s]+);?/igm, (m, c)=>{ + if (c === 'widget' || c.includes('-container')) return + extraCssClasses.push(c.replace(/"'/g,'')) + }) try { @@ -799,6 +807,19 @@ class Widget extends EventEmitter { this.container.insertBefore(style, this.widget) } + + // apply extra css classes + + if (this.extraCssClasses && this.extraCssClasses.length) { + this.container.classList.remove(...this.extraCssClasses) + this.extraCssClasses = [] + } + + if (extraCssClasses.length) { + this.container.classList.add(...extraCssClasses) + this.extraCssClasses = extraCssClasses + } + } if (styles.includes('color')) {