@blockquote-web-components/blockquote-base-style-helpers 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 blockquote-base-style-helpers
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.js ADDED
@@ -0,0 +1,103 @@
1
+ /**
2
+ ![Lit](https://img.shields.io/badge/lit-2.0.0-blue)
3
+
4
+ `BlockquoteBaseStyleHelpers` offers a set of helper functions for working with CSS.
5
+
6
+ There are two kinds of helps:
7
+
8
+ - Shared styles between components
9
+ - Document-level styles
10
+
11
+ ## Shared styles between components
12
+
13
+ [Adaptation of the Polymer ideas so that components can share styles, using native JS modules](https://polymer-library.polymer-project.org/2.0/docs/devguide/style-shadow-dom#share-styles-between-elements)
14
+
15
+ Using the shared styles is a two-step process:
16
+
17
+ - setComponentSharedStyles
18
+ - getComponentSharedStyles
19
+
20
+ ### setComponentSharedStyles
21
+
22
+ Define a shared-style inside a JS module.
23
+
24
+ The id attribute specifies the name you'll use to reference your shared styles, multiple shared styles can be registered using the same ID.
25
+ These will be applied to the component in the order they were registered.
26
+
27
+ __shared-styles__
28
+
29
+ ```js
30
+ import { css } from 'lit';
31
+ import { setComponentSharedStyles } from blockquote-base-style-helpers.js;
32
+
33
+ setComponentSharedStyles('x-foo-shared-styles', css`
34
+ :host {
35
+ background-color: red;
36
+ }
37
+ p {
38
+ color: blue;
39
+ }
40
+ `);
41
+ ```
42
+
43
+ ### getComponentSharedStyles
44
+
45
+ A component can use `getComponentSharedStyles` for retrieving style blocks and add it to its local styles extending or overriding it.
46
+
47
+ __Component__
48
+
49
+ ```js
50
+ import { LitElement, html, css } from 'lit';
51
+ import { getComponentSharedStyles } from 'blockquote-base-style-helpers';
52
+
53
+ class XFoo extends LitElement {
54
+ static get styles() {
55
+ return [
56
+ css`...`,
57
+ getComponentSharedStyles('x-foo-shared-styles)
58
+ ];
59
+ }
60
+ }
61
+ ```
62
+
63
+ ## Document-level styles
64
+
65
+ [Adaptation of the Polymer ideas so for defining styles in the main document](https://polymer-library.polymer-project.org/2.0/docs/devguide/style-shadow-dom#custom-style)
66
+
67
+ ### Progressive Enhancement:
68
+
69
+ - support browsers without native `Shadow DOM`
70
+ - support browsers with native `Shadow DOM` but without `adoptedStyleSheets`
71
+ - support browsers with native `Shadow DOM` and `adoptedStyleSheets`
72
+
73
+ #### support browsers without native `Shadow DOM`
74
+
75
+ The responsibility is delegated to:
76
+
77
+ - [custom-style](https://polymer-library.polymer-project.org/2.0/docs/devguide/style-shadow-dom#custom-style)
78
+ - [customstyleinterface](https://github.com/webcomponents/polyfills/tree/master/packages/shadycss#about-customstyleinterface)
79
+
80
+ #### **support browsers with native `Shadow DOM`**
81
+
82
+ The responsibility is delegated to:
83
+
84
+ - [Lit - adoptedStyleSheets](https://github.com/lit/lit/blob/main/packages/reactive-element/src/css-tag.ts#L160)
85
+
86
+ ```js
87
+ import { css } from 'lit';
88
+ import { setDocumentCustomStyles } from 'document-styles';
89
+
90
+ setDocumentCustomStyles(css`
91
+ :root {
92
+ --bg-color: rgba(0, 0, 255, 0.5);
93
+ }
94
+ p {
95
+ background-color: rgba(255, 0, 0, 0.5);
96
+ padding: 0.25rem 0.5rem;
97
+ }
98
+ `);
99
+ ```
100
+ @tagname blockquote-base-style-helpers
101
+ @element blockquote-base-style-helpers
102
+ */
103
+ export class ReadmElement extends HTMLElement {}
package/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # blockquote-base-style-helpers
2
+
3
+ ![Lit](https://img.shields.io/badge/lit-2.0.0-blue)
4
+
5
+ `BlockquoteBaseStyleHelpers` offers a set of helper functions for working with CSS.
6
+
7
+ There are two kinds of helps:
8
+
9
+ - Shared styles between components
10
+ - Document-level styles
11
+
12
+ ## Shared styles between components
13
+
14
+ [Adaptation of the Polymer ideas so that components can share styles, using native JS modules](https://polymer-library.polymer-project.org/2.0/docs/devguide/style-shadow-dom#share-styles-between-elements)
15
+
16
+ Using the shared styles is a two-step process:
17
+
18
+ - setComponentSharedStyles
19
+ - getComponentSharedStyles
20
+
21
+ ### setComponentSharedStyles
22
+
23
+ Define a shared-style inside a JS module.
24
+
25
+ The id attribute specifies the name you'll use to reference your shared styles, multiple shared styles can be registered using the same ID.
26
+ These will be applied to the component in the order they were registered.
27
+
28
+ __shared-styles__
29
+
30
+ ```js
31
+ import { css } from 'lit';
32
+ import { setComponentSharedStyles } from blockquote-base-style-helpers.js;
33
+
34
+ setComponentSharedStyles('x-foo-shared-styles', css`
35
+ :host {
36
+ background-color: red;
37
+ }
38
+ p {
39
+ color: blue;
40
+ }
41
+ `);
42
+ ```
43
+
44
+ ### getComponentSharedStyles
45
+
46
+ A component can use `getComponentSharedStyles` for retrieving style blocks and add it to its local styles extending or overriding it.
47
+
48
+ __Component__
49
+
50
+ ```js
51
+ import { LitElement, html, css } from 'lit';
52
+ import { getComponentSharedStyles } from 'blockquote-base-style-helpers';
53
+
54
+ class XFoo extends LitElement {
55
+ static get styles() {
56
+ return [
57
+ css`...`,
58
+ getComponentSharedStyles('x-foo-shared-styles)
59
+ ];
60
+ }
61
+ }
62
+ ```
63
+
64
+ ## Document-level styles
65
+
66
+ [Adaptation of the Polymer ideas so for defining styles in the main document](https://polymer-library.polymer-project.org/2.0/docs/devguide/style-shadow-dom#custom-style)
67
+
68
+ ### Progressive Enhancement:
69
+
70
+ - support browsers without native `Shadow DOM`
71
+ - support browsers with native `Shadow DOM` but without `adoptedStyleSheets`
72
+ - support browsers with native `Shadow DOM` and `adoptedStyleSheets`
73
+
74
+ #### support browsers without native `Shadow DOM`
75
+
76
+ The responsibility is delegated to:
77
+
78
+ - [custom-style](https://polymer-library.polymer-project.org/2.0/docs/devguide/style-shadow-dom#custom-style)
79
+ - [customstyleinterface](https://github.com/webcomponents/polyfills/tree/master/packages/shadycss#about-customstyleinterface)
80
+
81
+ #### **support browsers with native `Shadow DOM`**
82
+
83
+ The responsibility is delegated to:
84
+
85
+ - [Lit - adoptedStyleSheets](https://github.com/lit/lit/blob/main/packages/reactive-element/src/css-tag.ts#L160)
86
+
87
+ ```js
88
+ import { css } from 'lit';
89
+ import { setDocumentCustomStyles } from 'document-styles';
90
+
91
+ setDocumentCustomStyles(css`
92
+ :root {
93
+ --bg-color: rgba(0, 0, 255, 0.5);
94
+ }
95
+ p {
96
+ background-color: rgba(255, 0, 0, 0.5);
97
+ padding: 0.25rem 0.5rem;
98
+ }
99
+ `);
100
+ ```
package/index.js ADDED
@@ -0,0 +1,9 @@
1
+ export {
2
+ setDocumentStyles,
3
+ supportCustomStyleInterface,
4
+ supportsAdoptingStyleSheets,
5
+ documentAdoptStyles,
6
+ } from './src/setDocumentStyles.js';
7
+
8
+ export { setComponentSharedStyles } from './src/setComponentSharedStyles.js';
9
+ export { getComponentSharedStyles } from './src/getComponentSharedStyles.js';
package/package.json ADDED
@@ -0,0 +1,140 @@
1
+ {
2
+ "name": "@blockquote-web-components/blockquote-base-style-helpers",
3
+ "version": "1.0.0",
4
+ "description": "Webcomponent blockquote-base-style-helpers following open-wc recommendations",
5
+ "keywords": [
6
+ "lit",
7
+ "web-component",
8
+ "lit-element"
9
+ ],
10
+ "license": "MIT",
11
+ "author": "blockquote-base-style-helpers",
12
+ "main": "index.js",
13
+ "module": "index.js",
14
+ "files": [
15
+ "/define/",
16
+ "/src/",
17
+ "index.js",
18
+ "!/**/*.scss"
19
+ ],
20
+ "scripts": {
21
+ "analyze": "cem analyze --litelement --globs \"{src,define}/**/*.{js,ts}\" \"index.js\"",
22
+ "analyze:doc": "npm run analyze && npx web-component-analyzer \"{src,define}/**/*.{js,ts}\" \"index.js\" \"README.js\" --outFile README.md",
23
+ "build": "echo \"This is not a TypeScript project, so no need to build.\"",
24
+ "dev:vite": "vite build",
25
+ "format": "npm run format:eslint && npm run format:prettier && npm run format:stylelint",
26
+ "format:eslint": "eslint \"**/*.{js,ts,html}\" --fix --ignore-path .eslintignore",
27
+ "format:prettier": "prettier \"**/*.{js,ts,json,html}\" --write --ignore-path .eslintignore",
28
+ "format:stylelint": "stylelint \"**/*.{scss,css}\" --fix --allow-empty-input --ignore-path .eslintignore",
29
+ "postinstall": "npm run sort:package",
30
+ "lint": "npm run lint:eslint && npm run lint:prettier && npm run lint:stylelint",
31
+ "lint:eslint": "eslint \"**/*.{js,ts,html}\" --ignore-path .eslintignore",
32
+ "lint:prettier": "prettier \"**/*.{js,ts,json,html}\" --check --ignore-path .eslintignore",
33
+ "lint:stylelint": "stylelint \"**/*.{scss,css}\" --allow-empty-input --ignore-path .eslintignore",
34
+ "preview:vite": "vite preview",
35
+ "sass:watch": "sass-style-template",
36
+ "sort:package": "npx sort-package-json",
37
+ "start": "concurrently -k -r \"npm:sass:watch\" \"npm:vite\"",
38
+ "start:wds": "concurrently -k -r \"npm:sass:watch\" \"npm:wds\"",
39
+ "test": "wtr --coverage",
40
+ "test:watch": "wtr --watch",
41
+ "vite": "vite",
42
+ "wds": "web-dev-server"
43
+ },
44
+ "husky": {
45
+ "hooks": {
46
+ "pre-commit": "lint-staged"
47
+ }
48
+ },
49
+ "lint-staged": {
50
+ "**/*.{js,ts,html}": [
51
+ "npm run format:eslint"
52
+ ],
53
+ "**/*.{js,ts,json,html}": [
54
+ "npm run format:prettier"
55
+ ],
56
+ "**/*.{scss,css}": [
57
+ "npm run format:stylelint"
58
+ ]
59
+ },
60
+ "prettier": {
61
+ "arrowParens": "avoid",
62
+ "printWidth": 100,
63
+ "singleQuote": true,
64
+ "trailingComma": "all",
65
+ "overrides": [
66
+ {
67
+ "files": "*.{scss,css}",
68
+ "options": {
69
+ "printWidth": 280,
70
+ "singleQuote": false
71
+ }
72
+ }
73
+ ]
74
+ },
75
+ "eslintConfig": {
76
+ "parserOptions": {
77
+ "ecmaVersion": "latest"
78
+ },
79
+ "plugins": [
80
+ "log-filenames"
81
+ ],
82
+ "extends": [
83
+ "@open-wc",
84
+ "prettier"
85
+ ],
86
+ "rules": {
87
+ "class-methods-use-this": "off",
88
+ "no-unused-expressions": [
89
+ "error",
90
+ {
91
+ "allowShortCircuit": true,
92
+ "allowTernary": true
93
+ }
94
+ ],
95
+ "object-curly-newline": "off",
96
+ "import/extensions": [
97
+ "error",
98
+ "always",
99
+ {
100
+ "ignorePackages": true
101
+ }
102
+ ],
103
+ "import/no-extraneous-dependencies": [
104
+ "error",
105
+ {
106
+ "devDependencies": [
107
+ "**/test/**/*.{js,ts}",
108
+ "**/*.config.{js,ts,mjs,cjs}",
109
+ "**/*.conf.{js,ts,mjs,cjs}"
110
+ ]
111
+ }
112
+ ],
113
+ "import/no-unresolved": "off",
114
+ "import/prefer-default-export": "off"
115
+ }
116
+ },
117
+ "stylelint": {
118
+ "extends": "stylelint-config-standard-scss",
119
+ "rules": {
120
+ "custom-property-pattern": null,
121
+ "max-line-length": null,
122
+ "no-duplicate-selectors": null,
123
+ "color-function-notation": null,
124
+ "alpha-value-notation": null
125
+ }
126
+ },
127
+ "dependencies": {
128
+ "@blockquote-web-components/blockquote-base-meta": "^1.0.5",
129
+ "@blockquote/polymer": "^3.4.1",
130
+ "lit": "^2.0.2"
131
+ },
132
+ "devDependencies": {
133
+ "@blockquote-web-components/blockquote-base-common-dev-dependencies": "^1.2.1"
134
+ },
135
+ "publishConfig": {
136
+ "access": "public"
137
+ },
138
+ "customElements": "custom-elements.json",
139
+ "gitHead": "f0d88eaef7c6b48711470dd2ff5c7c3eb554e0ec"
140
+ }
@@ -0,0 +1,23 @@
1
+ import { css, unsafeCSS } from 'lit';
2
+ import { BlockquoteBaseMeta } from '@blockquote-web-components/blockquote-base-meta';
3
+
4
+ const meta = new BlockquoteBaseMeta({
5
+ type: 'sharedStyles',
6
+ });
7
+
8
+ /**
9
+ * Get styles (as cssResult) already associated to provided ID string (using setComponentSharedStyles helper) and returns them
10
+ * @param {String} id Identifier of styles; usually will be `[component-name]-shared-styles`
11
+ * @returns CSSResult
12
+ */
13
+ export const getComponentSharedStyles = id => {
14
+ const sharedStyles = meta.byKey(id);
15
+ if (sharedStyles) {
16
+ const onlyCSSResult = sharedStyles.filter(cssResultObject => cssResultObject.cssText);
17
+ const onlyUnsafeCssResult = unsafeCSS(onlyCSSResult.join(''));
18
+ return css`
19
+ ${onlyUnsafeCssResult}
20
+ `;
21
+ }
22
+ return css``;
23
+ };
@@ -0,0 +1,21 @@
1
+ import { BlockquoteBaseMeta } from '@blockquote-web-components/blockquote-base-meta';
2
+
3
+ /**
4
+ * Set styles (as cssResult) associated to provided ID string which can then be retrieved by components
5
+ * @param {String} id Identifier of styles; usually will be `[component-name]-shared-styles`
6
+ * @param {CSSResult} styles Styles to add, wrapper in a Lit css tagged template literal
7
+ */
8
+ export const setComponentSharedStyles = (id, styles) => {
9
+ /* c8 ignore if */
10
+ if (!styles.cssText) {
11
+ throw new Error(
12
+ `Value passed to 'setComponentSharedStyles' function must be a 'css' function result`,
13
+ );
14
+ }
15
+ const meta = new BlockquoteBaseMeta({
16
+ type: 'sharedStyles',
17
+ key: id,
18
+ });
19
+ meta.value = meta.value || [];
20
+ meta.value.push(styles);
21
+ };
@@ -0,0 +1,45 @@
1
+ import { supportsAdoptingStyleSheets /* adoptStyles */ } from 'lit';
2
+ import '@blockquote/polymer/lib/elements/custom-style.js';
3
+
4
+ export { supportsAdoptingStyleSheets /* adoptStyles */ } from 'lit';
5
+ export const supportCustomStyleInterface =
6
+ window.ShadyCSS && /* c8 ignore next */ window.ShadyCSS.CustomStyleInterface;
7
+
8
+ const renderDocumentRoot = supportsAdoptingStyleSheets
9
+ ? document
10
+ : /* c8 ignore next */ document.head;
11
+
12
+ export const documentAdoptStyles = (renderRoot, styles) => {
13
+ if (supportsAdoptingStyleSheets) {
14
+ // eslint-disable-next-line no-param-reassign
15
+ renderRoot.adoptedStyleSheets = renderRoot.adoptedStyleSheets.concat(
16
+ styles.map(s => (s instanceof CSSStyleSheet ? s : s.styleSheet)),
17
+ ); /* TODO PR css-tag.ts */
18
+ } else {
19
+ styles.forEach(s => {
20
+ const style = document.createElement('style');
21
+ // eslint-disable-next-line dot-notation
22
+ const nonce = window['litNonce']; /* original css-tag.ts const nonce = global['litNonce'] */
23
+ if (nonce !== undefined) {
24
+ style.setAttribute('nonce', nonce);
25
+ }
26
+ style.textContent = s.cssText;
27
+ renderRoot.appendChild(style);
28
+ });
29
+ }
30
+ };
31
+
32
+ const documentCustomStyle = s => {
33
+ const customStyle = document.createElement('custom-style');
34
+ const style = document.createElement('style');
35
+ style.textContent = s.cssText;
36
+ customStyle.appendChild(style);
37
+ document.head.appendChild(customStyle);
38
+ return customStyle;
39
+ };
40
+
41
+ export const setDocumentStyles = styles => {
42
+ supportCustomStyleInterface
43
+ ? documentCustomStyle(styles)
44
+ : documentAdoptStyles(renderDocumentRoot, [styles]);
45
+ };