Skip to content

Commit 720110e

Browse files
authored
PRO-8588: Add styles cascade (#5196)
* Add styles cascade * Universal rendering
1 parent ea780ad commit 720110e

7 files changed

Lines changed: 82 additions & 16 deletions

File tree

‎packages/apostrophe/modules/@apostrophecms/styles/index.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = {
2323
// down the UI slightly but allows the use of a custom render function
2424
serverRendered: false
2525
},
26+
cascades: [ 'styles' ],
2627
commands(self) {
2728
return {
2829
add: {
@@ -55,7 +56,10 @@ module.exports = {
5556
}
5657
};
5758
},
58-
init(self, options) {
59+
async init(self, options) {
60+
const { default: render } = await import('./ui/universal/render.mjs');
61+
self.stylesheetRender = render;
62+
5963
self.apos.doc.addContextOperation({
6064
action: 'reset-styles-position',
6165
label: 'apostrophe:stylesResetPosition',

‎packages/apostrophe/modules/@apostrophecms/styles/lib/methods.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
const { createId } = require('@paralleldrive/cuid2');
22
const _ = require('lodash');
3-
const render = require('../utils/render');
43

5-
module.exports = self => {
4+
module.exports = (self) => {
65
return {
76
stylesheet(req) {
87
// Stylesheet node should be created only for logged in users.
@@ -49,7 +48,7 @@ module.exports = self => {
4948
];
5049
},
5150
getStylesheet(doc) {
52-
return render(self.schema, doc);
51+
return self.stylesheetRender(self.schema, doc);
5352
},
5453
addToAdminBar() {
5554
self.apos.adminBar.add(

‎packages/apostrophe/modules/@apostrophecms/styles/ui/apos/components/TheAposStyles.vue‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979

8080
<script>
8181
import AposThemeMixin from 'Modules/@apostrophecms/ui/mixins/AposThemeMixin';
82-
import renderCss from 'apostrophe/modules/@apostrophecms/styles/utils/render';
82+
import renderCss from '../../universal/render.mjs';
8383
import { klona } from 'klona';
8484
import postcss from 'postcss';
8585
import postcssPlugin from 'postcss-viewport-to-container-toggle';

packages/apostrophe/modules/@apostrophecms/styles/utils/customRenderers.js renamed to packages/apostrophe/modules/@apostrophecms/styles/ui/universal/customRenderers.mjs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
/**
2-
* Custom render functions for fields with nuanced data structures
3-
* @param {object} field - the original schema field
4-
* @param {*} value - the value of the schema field
5-
* @returns {{ field: object, rule: string }}
6-
* - object containing the (un)modified field and CSS rules
7-
*/
8-
module.exports = {
1+
export default {
2+
/**
3+
* Custom render functions for fields with nuanced data structures
4+
* @param {object} field - the original schema field
5+
* @param {*} value - the value of the schema field
6+
* @returns {{ field: object, rule: string }}
7+
* - object containing the (un)modified field and CSS rules
8+
*/
99
box: function(field, value) {
1010
let rule;
1111
const {

packages/apostrophe/modules/@apostrophecms/styles/utils/render.js renamed to packages/apostrophe/modules/@apostrophecms/styles/ui/universal/render.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const customRenderers = require('./customRenderers.js');
1+
import customRenderers from './customRenderers.mjs';
22
// Render a stylesheet from a given schema and doc. Returns
33
// a string. No dependencies, so it can be used both front and back end
44

5-
module.exports = function(schema, doc) {
5+
export default function(schema, doc) {
66
const styles = new Map();
77

88
const subset = schema.filter(field => field.selector);

‎packages/apostrophe/modules/@apostrophecms/widget-type/index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@
158158
const _ = require('lodash');
159159

160160
module.exports = {
161-
cascades: [ 'fields', 'widgetOperations' ],
161+
cascades: [ 'fields', 'styles', 'widgetOperations' ],
162162
options: {
163163
neverLoadSelf: true,
164164
initialModal: true,

‎packages/apostrophe/test/styles.js‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const t = require('../test-lib/test.js');
2+
const assert = require('assert/strict');
3+
4+
describe('Styles', function () {
5+
let apos;
6+
7+
this.timeout(t.timeout);
8+
9+
before(async function () {
10+
apos = await t.create({
11+
root: module,
12+
modules: {
13+
'@apostrophecms/styles': {
14+
styles(self, options) {
15+
return {
16+
add: {
17+
border: 'border',
18+
borderCard: {
19+
preset: 'border',
20+
label: 'Card Border',
21+
selector: '.card'
22+
}
23+
}
24+
};
25+
}
26+
},
27+
'test-widget': {
28+
extend: '@apostrophecms/widget-type',
29+
options: {
30+
label: 'Test Widget',
31+
styles: {
32+
add: {
33+
border: 'border',
34+
backgroundColor: {
35+
type: 'color',
36+
required: true,
37+
property: 'background-color'
38+
}
39+
}
40+
}
41+
}
42+
}
43+
}
44+
});
45+
});
46+
47+
after(async function () {
48+
return t.destroy(apos);
49+
});
50+
51+
it('@apostrophecms/styles should exist', async function () {
52+
assert(
53+
apos.modules['@apostrophecms/styles'],
54+
'@apostrophecms/styles module should exist'
55+
);
56+
assert(apos.styles, 'Alias `apos.styles` should exist');
57+
});
58+
59+
it('should cascade styles', async function () {
60+
assert(apos.modules['test-widget'].styles, 'test-widget should have a styles property');
61+
assert(apos.styles.styles, '@apostrophecms/styles should have a styles property');
62+
});
63+
});

0 commit comments

Comments
 (0)