@teleporthq/teleport-plugin-react-jss 0.18.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) 2020 teleporthq
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.md ADDED
@@ -0,0 +1,14 @@
1
+ # teleport-plugin-react-jss
2
+
3
+ A plugin for injecting the styles with JSS inside React components.
4
+
5
+ > This package is part of the [teleport ecosystem](https://github.com/teleporthq/teleport-code-generators). For a complete guide, check out the [official documentation](https://docs.teleporthq.io/).
6
+
7
+ ## Install
8
+ ```bash
9
+ npm install @teleporthq/teleport-plugin-react-jss
10
+ ```
11
+ or
12
+ ```bash
13
+ yarn add @teleporthq/teleport-plugin-react-jss
14
+ ```
@@ -0,0 +1,88 @@
1
+ import { createReactJSSPlugin } from '../src'
2
+ import { staticNode, elementNode, component } from '@teleporthq/teleport-uidl-builders'
3
+ import { ComponentStructure } from '@teleporthq/teleport-types'
4
+ import { createComponentChunk } from './mocks'
5
+
6
+ describe('Component Scoped Styles', () => {
7
+ const uidl = component('MYComponent', elementNode('container', {}, [], null, {}), {}, {})
8
+ uidl.styleSetDefinitions = {
9
+ 'primary-navbar': {
10
+ type: 'reusable-component-style-map',
11
+ content: {
12
+ width: staticNode('100px'),
13
+ height: staticNode('200px'),
14
+ },
15
+ },
16
+ secondaryNavbar: {
17
+ type: 'reusable-component-style-map',
18
+ content: {
19
+ height: staticNode('50px'),
20
+ width: staticNode('50px'),
21
+ },
22
+ },
23
+ }
24
+
25
+ it('Generates component-scoped style sheet', async () => {
26
+ const plugin = createReactJSSPlugin()
27
+ const structure: ComponentStructure = {
28
+ uidl,
29
+ chunks: [createComponentChunk()],
30
+ dependencies: {},
31
+ options: {},
32
+ }
33
+ const { chunks } = await plugin(structure)
34
+ const styleChunk = chunks.find((chunk) => chunk.name === 'jss-style-definition')
35
+ const expression = styleChunk.content.declarations[0].init
36
+ const properties = expression.arguments[0].properties
37
+
38
+ expect(styleChunk).toBeDefined()
39
+ expect(expression.callee.name).toBe('createUseStyles')
40
+ expect(properties.length).toBe(2)
41
+ expect(properties[0].key.value).toBe('primary-navbar')
42
+ expect(properties[1].key.value).toBe('secondaryNavbar')
43
+ })
44
+
45
+ it(`Generates Component-scoped style sheet and adds to the node`, async () => {
46
+ const plugin = createReactJSSPlugin()
47
+ uidl.node.content.referencedStyles = {
48
+ '12345678': {
49
+ type: 'style-map',
50
+ content: {
51
+ mapType: 'component-referenced',
52
+ content: staticNode('md-8'),
53
+ },
54
+ },
55
+ '1415161718': {
56
+ type: 'style-map',
57
+ content: {
58
+ mapType: 'component-referenced',
59
+ content: {
60
+ type: 'dynamic',
61
+ content: {
62
+ referenceType: 'prop',
63
+ id: 'variant',
64
+ },
65
+ },
66
+ },
67
+ },
68
+ }
69
+ const structure: ComponentStructure = {
70
+ uidl,
71
+ chunks: [createComponentChunk()],
72
+ dependencies: {},
73
+ options: {},
74
+ }
75
+
76
+ const { chunks } = await plugin(structure)
77
+ const jsxComp = chunks.find((chunk) => chunk.name === 'jsx-component')
78
+ const attrs = jsxComp.meta.nodesLookup.container.openingElement.attributes[0]
79
+ const attrExpressions = attrs.value.expression.expressions
80
+
81
+ expect(attrs.value.expression.quasis.length).toBe(3)
82
+ expect(attrExpressions.length).toBe(2)
83
+ expect(attrExpressions[0].name).toBe("'md-8'")
84
+ expect(attrExpressions[1].object.name).toBe('classes')
85
+ expect(attrExpressions[1].property.object.name).toBe('props')
86
+ expect(attrExpressions[1].property.property.name).toBe('variant')
87
+ })
88
+ })
@@ -0,0 +1,57 @@
1
+ import { component, elementNode, staticNode } from '@teleporthq/teleport-uidl-builders'
2
+ import { ComponentStructure, ChunkType } from '@teleporthq/teleport-types'
3
+ import { createReactJSSPlugin } from '../src/index'
4
+ import { createComponentChunk } from './mocks'
5
+
6
+ describe('plugin-react-jss', () => {
7
+ const plugin = createReactJSSPlugin({
8
+ styleChunkName: 'jss-chunk',
9
+ })
10
+ const componentChunk = createComponentChunk()
11
+
12
+ it('generates no chunk if no styles exist', async () => {
13
+ const uidlSample = component('JSS', elementNode('container'))
14
+ const structure: ComponentStructure = {
15
+ uidl: uidlSample,
16
+ options: {},
17
+ chunks: [componentChunk],
18
+ dependencies: {},
19
+ }
20
+
21
+ const { chunks } = await plugin(structure)
22
+
23
+ expect(chunks.length).toBe(1)
24
+ })
25
+
26
+ it('Should add react-jss as dependency', async () => {
27
+ const style = {
28
+ height: staticNode('100px'),
29
+ }
30
+ const element = elementNode(
31
+ 'container',
32
+ {},
33
+ [],
34
+ { type: 'package' as const, version: '10.4.0', path: 'react-jss' },
35
+ style
36
+ )
37
+ element.content.key = 'container'
38
+ const uidlSample = component('JSS', element)
39
+ const structure: ComponentStructure = {
40
+ uidl: uidlSample,
41
+ options: {},
42
+ chunks: [componentChunk],
43
+ dependencies: {},
44
+ }
45
+
46
+ const { dependencies, chunks } = await plugin(structure)
47
+ const { createUseStyles } = dependencies
48
+
49
+ expect(Object.keys(dependencies).length).toBeGreaterThan(0)
50
+ expect(createUseStyles.type).toBe('package')
51
+ expect(createUseStyles.path).toBe('react-jss')
52
+
53
+ expect(chunks.length).toBe(2)
54
+ expect(chunks[1].type).toBe(ChunkType.AST)
55
+ expect(chunks[1].name).toBe('jss-chunk')
56
+ })
57
+ })
@@ -0,0 +1,35 @@
1
+ import { ChunkDefinition, FileType, ChunkType } from '@teleporthq/teleport-types'
2
+
3
+ export const createComponentChunk = (): ChunkDefinition => ({
4
+ name: 'jsx-component',
5
+ meta: {
6
+ nodesLookup: {
7
+ container: {
8
+ openingElement: {
9
+ name: {
10
+ name: '',
11
+ },
12
+ attributes: [],
13
+ },
14
+ },
15
+ },
16
+ dynamicRefPrefix: {
17
+ prop: 'props',
18
+ },
19
+ },
20
+ type: ChunkType.AST,
21
+ fileType: FileType.JS,
22
+ linkAfter: ['import-local'],
23
+ content: {
24
+ declarations: [
25
+ {
26
+ init: {
27
+ params: [],
28
+ body: {
29
+ body: [],
30
+ },
31
+ },
32
+ },
33
+ ],
34
+ },
35
+ })
@@ -0,0 +1,118 @@
1
+ import { ComponentStructure, UIDLStyleSetDefinition } from '@teleporthq/teleport-types'
2
+ import { createReactJSSPlugin } from '../src'
3
+ import { component, elementNode, staticNode } from '@teleporthq/teleport-uidl-builders'
4
+ import { createComponentChunk } from './mocks'
5
+
6
+ describe('Referenced Styles on Node', () => {
7
+ const componentChunk = createComponentChunk()
8
+ const uidl = component('MyComponent', elementNode('container', null, [], null, null, null, null))
9
+
10
+ it('Media and pseudo styles are generated from referencedStyles', async () => {
11
+ const plugin = createReactJSSPlugin()
12
+ uidl.node.content.referencedStyles = {
13
+ '5ed659b1732f9b804f7b6381': {
14
+ type: 'style-map',
15
+ content: {
16
+ mapType: 'inlined',
17
+ conditions: [{ conditionType: 'screen-size', maxWidth: 991 }],
18
+ styles: {
19
+ display: staticNode('none'),
20
+ },
21
+ },
22
+ },
23
+ '5ed659b1732f9b804f7b6382': {
24
+ type: 'style-map',
25
+ content: {
26
+ mapType: 'inlined',
27
+ conditions: [{ conditionType: 'element-state', content: 'hover' }],
28
+ styles: {
29
+ display: staticNode('block'),
30
+ },
31
+ },
32
+ },
33
+ }
34
+ uidl.node.content.key = 'container'
35
+
36
+ const structure: ComponentStructure = {
37
+ uidl,
38
+ chunks: [componentChunk],
39
+ dependencies: {},
40
+ options: {},
41
+ }
42
+ const result = await plugin(structure)
43
+ const { chunks, dependencies } = result
44
+
45
+ expect(chunks.length).toBe(2)
46
+ expect(Object.keys(dependencies).length).toBe(1)
47
+ expect(dependencies.createUseStyles.path).toBe('react-jss')
48
+ })
49
+
50
+ it('References a style from project and adds, Media and pseudo from referencedStyles', async () => {
51
+ const plugin = createReactJSSPlugin()
52
+ uidl.node.content.referencedStyles = {
53
+ '5ed659b1732f9b804f7b6381': {
54
+ type: 'style-map',
55
+ content: {
56
+ mapType: 'inlined',
57
+ conditions: [{ conditionType: 'screen-size', maxWidth: 991 }],
58
+ styles: {
59
+ display: staticNode('none'),
60
+ },
61
+ },
62
+ },
63
+ '5ed659b1732f9b804f7b6382': {
64
+ type: 'style-map',
65
+ content: {
66
+ mapType: 'inlined',
67
+ conditions: [{ conditionType: 'element-state', content: 'hover' }],
68
+ styles: {
69
+ display: staticNode('block'),
70
+ },
71
+ },
72
+ },
73
+ '5ed659b1732f9b804f7b6384': {
74
+ type: 'style-map',
75
+ content: {
76
+ mapType: 'project-referenced',
77
+ referenceId: 'primaryButton',
78
+ },
79
+ },
80
+ }
81
+ uidl.node.content.key = 'container'
82
+
83
+ const structure: ComponentStructure = {
84
+ uidl,
85
+ chunks: [componentChunk],
86
+ dependencies: {},
87
+ options: {},
88
+ }
89
+
90
+ const styleSetDefinitions: Record<string, UIDLStyleSetDefinition> = {
91
+ primaryButton: {
92
+ type: 'reusable-project-style-map' as const,
93
+ content: {
94
+ background: staticNode('blue'),
95
+ color: staticNode('red'),
96
+ },
97
+ },
98
+ }
99
+
100
+ structure.options.projectStyleSet = {
101
+ styleSetDefinitions,
102
+ fileName: 'style',
103
+ path: '../',
104
+ }
105
+
106
+ const result = await plugin(structure)
107
+ const { chunks, dependencies } = result
108
+ const styleChunk = chunks.find((chunk) => chunk.name === 'jss-style-definition')
109
+ const properties = styleChunk.content.declarations[0].init.arguments[0]
110
+
111
+ expect(chunks.length).toBe(2)
112
+ expect(Object.keys(dependencies).length).toBe(2)
113
+ expect(dependencies.createUseStyles.path).toBe('react-jss')
114
+ expect(dependencies.useProjectStyles.path).toBe('../style')
115
+ expect(dependencies.useProjectStyles.meta.namedImport).toBe(true)
116
+ expect(properties.properties[0].value.properties[0].key.value).toBe('&:hover')
117
+ })
118
+ })
@@ -0,0 +1,122 @@
1
+ import {
2
+ ComponentStructure,
3
+ UIDLStyleSetDefinition,
4
+ UIDLDesignTokens,
5
+ } from '@teleporthq/teleport-types'
6
+ import { createStyleSheetPlugin } from '../src'
7
+ import { component, elementNode, staticNode } from '@teleporthq/teleport-uidl-builders'
8
+ import { createComponentChunk } from './mocks'
9
+
10
+ describe('Style Sheet from react-jss', () => {
11
+ const componentChunk = createComponentChunk()
12
+
13
+ it('Generates a style sheet from the give JSON of styleSet', async () => {
14
+ const plugin = createStyleSheetPlugin()
15
+ const uidl = component('MyComponent', elementNode('container'))
16
+ const tokens: UIDLDesignTokens = {
17
+ 'blue-500': {
18
+ type: 'static',
19
+ content: '#9999ff',
20
+ },
21
+ 'blue-600': {
22
+ type: 'static',
23
+ content: '#6b7db3',
24
+ },
25
+ 'red-500': {
26
+ type: 'static',
27
+ content: '#ff9999',
28
+ },
29
+ }
30
+ const structure: ComponentStructure = {
31
+ uidl,
32
+ chunks: [componentChunk],
33
+ options: {},
34
+ dependencies: {},
35
+ }
36
+ const styleSetDefinitions: Record<string, UIDLStyleSetDefinition> = {
37
+ primaryButton: {
38
+ type: 'reusable-project-style-map',
39
+ content: {
40
+ background: staticNode('blue'),
41
+ color: staticNode('red'),
42
+ },
43
+ },
44
+ secondaryButton: {
45
+ type: 'reusable-project-style-map',
46
+ content: {
47
+ background: staticNode('red'),
48
+ color: staticNode('blue'),
49
+ },
50
+ },
51
+ conditionalButton: {
52
+ type: 'reusable-project-style-map',
53
+ conditions: [
54
+ {
55
+ type: 'screen-size',
56
+ meta: { maxWidth: 991 },
57
+ content: {
58
+ backgrouns: staticNode('purple'),
59
+ },
60
+ },
61
+ {
62
+ type: 'element-state',
63
+ meta: { state: 'hover' },
64
+ content: {
65
+ background: staticNode('yellow'),
66
+ },
67
+ },
68
+ ],
69
+ content: {
70
+ background: staticNode('red'),
71
+ color: staticNode('blue'),
72
+ },
73
+ },
74
+ }
75
+
76
+ structure.uidl = {
77
+ ...structure.uidl,
78
+ styleSetDefinitions,
79
+ designLanguage: {
80
+ tokens,
81
+ },
82
+ }
83
+
84
+ const result = await plugin(structure)
85
+ const { chunks, dependencies } = result
86
+
87
+ expect(chunks.length).toBe(3)
88
+ expect(dependencies.createUseStyles.path).toBe('react-jss')
89
+ })
90
+
91
+ it('Changes the name of output file, with the name is passed', async () => {
92
+ const plugin = createStyleSheetPlugin({ fileName: 'index' })
93
+ const uidl = component('MyComponent', elementNode('container'))
94
+ const structure: ComponentStructure = {
95
+ uidl,
96
+ chunks: [componentChunk],
97
+ options: {},
98
+ dependencies: {},
99
+ }
100
+ structure.uidl.styleSetDefinitions = {
101
+ primaryButton: {
102
+ type: 'reusable-project-style-map',
103
+ content: {
104
+ background: staticNode('blue'),
105
+ color: staticNode('red'),
106
+ },
107
+ },
108
+ secondaryButton: {
109
+ type: 'reusable-project-style-map',
110
+ content: {
111
+ background: staticNode('red'),
112
+ color: staticNode('blue'),
113
+ },
114
+ },
115
+ }
116
+
117
+ const result = await plugin(structure)
118
+ const { chunks } = result
119
+ const styleChunk = chunks.find((chunk) => chunk.name === 'index')
120
+ expect(styleChunk).toBeDefined()
121
+ })
122
+ })
@@ -0,0 +1,14 @@
1
+ import { ComponentPluginFactory, ComponentPlugin } from '@teleporthq/teleport-types';
2
+ import { createStyleSheetPlugin } from './style-sheet';
3
+ interface JSSConfig {
4
+ styleChunkName?: string;
5
+ importChunkName?: string;
6
+ componentChunkName: string;
7
+ jssDeclarationName?: string;
8
+ classAttributeName?: string;
9
+ styleObjectImportName?: string;
10
+ }
11
+ export declare const createReactJSSPlugin: ComponentPluginFactory<JSSConfig>;
12
+ export { createStyleSheetPlugin };
13
+ declare const _default: ComponentPlugin;
14
+ export default _default;