@streamlinedfi/div 0.1.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) 2025 Streamlined Finance - www.streamlined.finance
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,2 @@
1
+ # div
2
+ Streamlined Div is a styled-components extension of the html div for building products with speed
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@streamlinedfi/div",
3
+ "version": "0.1.0",
4
+ "description": "Streamlined Div is a styled-components extension of the html div for building products with speed",
5
+ "author": "Streamlined Finance",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/streamlinedfi/div.git"
9
+ },
10
+ "license": "MIT",
11
+ "bugs": {
12
+ "url": "https://github.com/streamlinedfi/div/issues"
13
+ },
14
+ "homepage": "https://github.com/streamlinedfi/div#readme",
15
+ "main": "src/index.js",
16
+ "scripts": {
17
+ "test": "echo \"Error: no test specified\" && exit 1"
18
+ },
19
+ "peerDependencies": {
20
+ "styled-components": "^6.1.19"
21
+ }
22
+ }
package/src/Div.js ADDED
@@ -0,0 +1,132 @@
1
+ import styled from 'styled-components';
2
+ import { cssWhiteList, divRules } from './rules';
3
+ import {
4
+ getMediaPropRegex,
5
+ getRuleIndex,
6
+ kebabCase,
7
+ upperFirst,
8
+ } from './utils';
9
+
10
+ const { keys } = Object;
11
+
12
+ /**
13
+ * @example
14
+ * <Div
15
+ * $w={690}
16
+ * $h={420}
17
+ * $mobile$w={320}
18
+ * />
19
+ */
20
+
21
+ export function renderCss(props, rules) {
22
+ let css = '';
23
+
24
+ const ruleIndex = getRuleIndex(rules);
25
+ const mediaPropRegex = getMediaPropRegex(props.theme);
26
+
27
+ let i = -1;
28
+ const propKeys = keys(props);
29
+ while (++i < propKeys.length) {
30
+ const propExt = propKeys[i];
31
+
32
+ if (/^\$/.test(propExt)) {
33
+ const value = props[propExt];
34
+
35
+ if (value !== false) {
36
+ const match = propExt.match(mediaPropRegex) || [];
37
+ const [, media, mediaPropExt] = match;
38
+
39
+ const cleanProp = (mediaPropExt || propExt).slice(1);
40
+ const parts = cleanProp.split('$');
41
+ const prop = parts[0];
42
+ const extensions = parts.slice(1);
43
+
44
+ const foundRule =
45
+ ruleIndex[prop] || (cssWhiteList[prop] && kebabCase(prop));
46
+
47
+ if (foundRule) {
48
+ const config = foundRule;
49
+ const outputValue =
50
+ config.withTheme && typeof value === 'function'
51
+ ? value(props.theme)
52
+ : value;
53
+ const formattedValue = config.format
54
+ ? config.format(outputValue, props.theme)
55
+ : outputValue;
56
+
57
+ let ruleCss = '';
58
+ if (typeof config === 'string') {
59
+ ruleCss = config;
60
+ } else if (typeof config === 'function') {
61
+ ruleCss = config(props, media, extensions);
62
+ } else if (typeof config.css === 'function') {
63
+ ruleCss = config.css(props, media, extensions);
64
+ } else {
65
+ ruleCss = config.css;
66
+ }
67
+
68
+ let cssString = '';
69
+ const colons = ruleCss.match(/:/g);
70
+ if (colons && colons.length > 1) {
71
+ cssString = ruleCss;
72
+ } else if (colons === null) {
73
+ cssString = `${ruleCss}: ${formattedValue};`;
74
+ } else {
75
+ cssString = `${ruleCss.replace(/;$/, '')};`;
76
+ }
77
+
78
+ if (extensions.includes('important')) {
79
+ cssString = cssString.replace(/;/g, ' !important;');
80
+ }
81
+
82
+ if (extensions.includes('hover') && !extensions.includes('focus')) {
83
+ cssString = `&:hover,&:focus { ${cssString} }`;
84
+ }
85
+
86
+ if (extensions.includes('hover') && extensions.includes('focus')) {
87
+ cssString = `&:hover { ${cssString} }`;
88
+ }
89
+
90
+ if (extensions.includes('focus')) {
91
+ cssString = `&:focus { ${cssString} }`;
92
+ }
93
+
94
+ if (extensions.includes('active')) {
95
+ cssString = `&:active { ${cssString} }`;
96
+ }
97
+
98
+ if (extensions.includes('last')) {
99
+ cssString = `&:last-child { ${cssString} }`;
100
+ }
101
+
102
+ if (extensions.includes('odd')) {
103
+ cssString = `&:nth-child(odd) { ${cssString} }`;
104
+ }
105
+
106
+ if (extensions.includes('even')) {
107
+ cssString = `&:nth-child(even) { ${cssString} }`;
108
+ }
109
+
110
+ if (media) {
111
+ const cssMedia = props.theme.Breakpoints.getCssMedia(
112
+ props.theme.Breakpoints[upperFirst(media)],
113
+ );
114
+ cssString = `${cssMedia} { ${cssString} }`;
115
+ }
116
+
117
+ css += cssString;
118
+ }
119
+ }
120
+ }
121
+ }
122
+
123
+ return css;
124
+ }
125
+
126
+ export const divMixin = props => renderCss(props, divRules);
127
+
128
+ const Div = styled.div`
129
+ ${props => divMixin(props)}
130
+ `;
131
+
132
+ export default Div;
@@ -0,0 +1,58 @@
1
+ import { upperFirst } from './utils';
2
+
3
+ const { keys, entries, fromEntries } = Object;
4
+
5
+ const getDefaultWidth = () => {
6
+ return typeof window !== 'undefined' && typeof document !== 'undefined'
7
+ ? document.body.clientWidth
8
+ : 0;
9
+ };
10
+
11
+ const createBreakpoints = breakpoints => {
12
+ return {
13
+ getCssMedia: ({ min, max }) => {
14
+ if (min && max) {
15
+ return `@media (min-width: ${min}px) and (max-width: ${max}px)`;
16
+ }
17
+
18
+ if (min) {
19
+ return `@media (min-width: ${min}px)`;
20
+ }
21
+
22
+ return `@media (max-width: ${max}px)`;
23
+ },
24
+ keys: keys(breakpoints),
25
+ ...fromEntries(
26
+ entries(breakpoints).map(([key, { min, max }]) => {
27
+ return [upperFirst(key), { min, max }];
28
+ }),
29
+ ),
30
+ ...fromEntries(
31
+ entries(breakpoints).map(([key, { min = 0, max = Infinity }]) => {
32
+ return [
33
+ `is${upperFirst(key)}`,
34
+ (width = getDefaultWidth()) =>
35
+ width !== 0 && width >= min && width <= max,
36
+ ];
37
+ }),
38
+ ),
39
+ };
40
+ };
41
+
42
+ const createSpacing = spacingUnit => (multiplier, adjuster = 0, usePx = true) =>
43
+ (multiplier || 1) * spacingUnit + adjuster + (usePx ? 'px' : 0);
44
+
45
+ function createUISystem({ theme, breakpoints, spacingUnit }) {
46
+ const Breakpoints = createBreakpoints(breakpoints);
47
+
48
+ return {
49
+ theme: {
50
+ ...theme,
51
+ Breakpoints,
52
+ spacing: createSpacing(spacingUnit),
53
+ },
54
+ Breakpoints,
55
+ };
56
+ }
57
+
58
+ export default createUISystem;
package/src/index.js ADDED
@@ -0,0 +1,6 @@
1
+ import Div from './Div';
2
+ import createUISystem from './createUISystem';
3
+
4
+ export default Div;
5
+
6
+ export { createUISystem };
package/src/rules.js ADDED
@@ -0,0 +1,371 @@
1
+ /* eslint-disable no-nested-ternary */
2
+ import { getPropValue, isPropPresent, pixelate, spacing } from './utils';
3
+
4
+ const { fromEntries } = Object;
5
+
6
+ export const cssWhiteList = fromEntries(
7
+ [
8
+ 'display',
9
+ 'align',
10
+ 'vAlign',
11
+ 'alignItems',
12
+ 'alignContent',
13
+ 'alignSelf',
14
+ 'justifyContent',
15
+ 'justifySelf',
16
+ 'flexDirection',
17
+ 'flexBasis',
18
+ 'flexWrap',
19
+ 'pointerEvents',
20
+ 'cursor',
21
+ 'position',
22
+ 'opacity',
23
+ 'backgroundColor',
24
+ 'backgroundImage',
25
+ 'backgroundSize',
26
+ 'backgroundPosition',
27
+ 'backgroundRepeat',
28
+ 'transform',
29
+ 'transformOrigin',
30
+ 'textDecoration',
31
+ 'textWrap',
32
+ 'animation',
33
+ 'fontWeight',
34
+ 'gap',
35
+ 'gridTemplateColumns',
36
+ 'gridTemplateRows',
37
+ 'wordBreak',
38
+ 'whiteSpace',
39
+ 'textOverflow',
40
+ 'overflow',
41
+ 'overflowX',
42
+ 'overflowY',
43
+ 'textAlign',
44
+ 'transitionDelay',
45
+ 'filter',
46
+ 'listStyle',
47
+ 'objectFit',
48
+ 'objectPosition',
49
+ 'willChange',
50
+ 'boxSizing',
51
+ ].map(prop => [prop, 1]),
52
+ );
53
+
54
+ // short hand rules for the div component
55
+ export const divRules = {
56
+ inline: 'display: inline',
57
+ block: 'display: block',
58
+ flex: `
59
+ display: flex;
60
+ width: 100%;
61
+ `,
62
+ autoFlex: `
63
+ display: flex;
64
+ width: auto;
65
+ `,
66
+ grid: `
67
+ display: grid;
68
+ `,
69
+ hide: 'display: none',
70
+ inlineBlock: 'display: inline-block',
71
+ inlineFlex: 'display: inline-flex',
72
+ 'column|col': 'flex-direction: column;',
73
+ row: 'flex-direction: row;',
74
+ spaceBetween: 'justify-content: space-between;',
75
+ wrap: 'flex-wrap: wrap;',
76
+ innerCenter: (props, media) =>
77
+ isPropPresent(props, media, 'flex', 'inlineFlex')
78
+ ? `
79
+ justify-content: center;
80
+ align-items: center;
81
+ `
82
+ : `
83
+ text-align: center;
84
+ ${
85
+ isPropPresent(props, media, 'h', 'height')
86
+ ? `line-height: ${getPropValue(props, media, 'h', 'height')};`
87
+ : ''
88
+ }
89
+ `,
90
+ innerLeft: (props, media) =>
91
+ isPropPresent(props, media, 'flex', 'inlineFlex')
92
+ ? isPropPresent(props, media, 'column', 'col')
93
+ ? 'align-items: flex-start;'
94
+ : 'justify-content: flex-start;'
95
+ : 'text-align: left;',
96
+ innerRight: (props, media) =>
97
+ isPropPresent(props, media, 'flex', 'inlineFlex')
98
+ ? isPropPresent(props, media, 'column', 'col')
99
+ ? 'align-items: flex-end;'
100
+ : 'justify-content: flex-end;'
101
+ : 'text-align: right;',
102
+ innerTop: (props, media) => {
103
+ return isPropPresent(props, media, 'flex', 'inlineFlex')
104
+ ? isPropPresent(props, media, 'column', 'col')
105
+ ? 'justify-content: flex-start;'
106
+ : 'align-items: flex-start;'
107
+ : '';
108
+ },
109
+ innerBottom: (props, media) =>
110
+ isPropPresent(props, media, 'flex', 'inlineFlex')
111
+ ? isPropPresent(props, media, 'column', 'col')
112
+ ? 'justify-content: flex-end;'
113
+ : 'align-items: flex-end;'
114
+ : '',
115
+ outerCenter: `
116
+ margin-left: auto;
117
+ margin-right: auto;
118
+ `,
119
+ grow: {
120
+ css: 'flex-grow',
121
+ format: Number,
122
+ },
123
+ shrink: {
124
+ css: 'flex-shrink',
125
+ },
126
+ 'colReverse|columnReverse': {
127
+ css: 'flex-direction: column-reverse;',
128
+ },
129
+ reverse: {
130
+ css: 'flex-direction: row-reverse;',
131
+ },
132
+
133
+ // dimensions
134
+ 'width|w': {
135
+ css: 'width',
136
+ format: pixelate,
137
+ },
138
+ 'minWidth|minW': {
139
+ css: 'min-width',
140
+ format: pixelate,
141
+ },
142
+ 'maxWidth|maxW': {
143
+ css: (props, media) => `
144
+ width: 100%;
145
+ max-width: ${pixelate(getPropValue(props, media, 'maxWidth', 'maxW'))};
146
+ `,
147
+ },
148
+ 'height|h': {
149
+ css: 'height',
150
+ format: pixelate,
151
+ },
152
+ 'minHeight|minH': {
153
+ css: 'min-height',
154
+ format: pixelate,
155
+ },
156
+ 'maxHeight|maxH': {
157
+ css: (props, media) => `
158
+ height: 100%;
159
+ max-height: ${pixelate(getPropValue(props, media, 'maxHeight', 'maxH'))};
160
+ `,
161
+ },
162
+ 'lineHeight|lineH|lh': {
163
+ css: 'line-height',
164
+ format: pixelate,
165
+ },
166
+ pointer: 'cursor: pointer;',
167
+
168
+ // position
169
+ relative: 'position: relative',
170
+ absolute: 'position: absolute',
171
+ fixed: 'position: fixed',
172
+ static: 'position: static',
173
+ cover: `
174
+ position: absolute;
175
+ top: 0;
176
+ left: 0;
177
+ width: 100%;
178
+ height: 100%;
179
+ `,
180
+ 'zIndex|z': {
181
+ css: 'z-index',
182
+ withTheme: true,
183
+ },
184
+ top: {
185
+ css: 'top',
186
+ format: pixelate,
187
+ },
188
+ right: {
189
+ css: 'right',
190
+ format: pixelate,
191
+ },
192
+ bottom: {
193
+ css: 'bottom',
194
+ format: pixelate,
195
+ },
196
+ left: {
197
+ css: 'left',
198
+ format: pixelate,
199
+ },
200
+
201
+ 'padding|p': {
202
+ css: 'padding',
203
+ format: spacing,
204
+ },
205
+ 'paddingTop|pt': {
206
+ css: 'padding-top',
207
+ format: spacing,
208
+ },
209
+ 'paddingRight|pr': {
210
+ css: 'padding-right',
211
+ format: spacing,
212
+ },
213
+ 'paddingBottom|pb': {
214
+ css: 'padding-bottom',
215
+ format: spacing,
216
+ },
217
+ 'paddingLeft|pl': {
218
+ css: 'padding-left',
219
+ format: spacing,
220
+ },
221
+ 'paddingX|px': {
222
+ css: (props, media) => `
223
+ padding-left: ${spacing(getPropValue(props, media, 'paddingX', 'px'))};
224
+ padding-right: ${spacing(getPropValue(props, media, 'paddingX', 'px'))};
225
+ `,
226
+ },
227
+ 'paddingY|py': {
228
+ css: (props, media) => `
229
+ padding-top: ${spacing(getPropValue(props, media, 'paddingY', 'py'))};
230
+ padding-bottom: ${spacing(getPropValue(props, media, 'paddingY', 'py'))};
231
+ `,
232
+ },
233
+ 'margin|m': {
234
+ css: 'margin',
235
+ format: spacing,
236
+ },
237
+ 'marginTop|mt': {
238
+ css: 'margin-top',
239
+ format: spacing,
240
+ },
241
+ 'marginRight|mr': {
242
+ css: 'margin-right',
243
+ format: spacing,
244
+ },
245
+ 'marginBottom|mb': {
246
+ css: 'margin-bottom',
247
+ format: spacing,
248
+ },
249
+ 'marginLeft|ml': {
250
+ css: 'margin-left',
251
+ format: spacing,
252
+ },
253
+ 'marginX|mx': {
254
+ css: (props, media) => `
255
+ margin-left: ${spacing(getPropValue(props, media, 'marginX', 'mx'))};
256
+ margin-right: ${spacing(getPropValue(props, media, 'marginX', 'mx'))};
257
+ `,
258
+ },
259
+ 'marginY|my': {
260
+ css: (props, media) => `
261
+ margin-top: ${spacing(getPropValue(props, media, 'marginY', 'my'))};
262
+ margin-bottom: ${spacing(getPropValue(props, media, 'marginY', 'my'))};
263
+ `,
264
+ },
265
+
266
+ // style
267
+ 'background|bg': {
268
+ css: 'background',
269
+ withTheme: true,
270
+ },
271
+ 'borderRadius|radius': {
272
+ css: (props, media) => {
273
+ const radius = getPropValue(props, media, 'borderRadius', 'radius');
274
+ if (Array.isArray(radius)) {
275
+ return `
276
+ border-top-left-radius: ${pixelate(radius[0])};
277
+ border-top-right-radius: ${pixelate(radius[1])};
278
+ border-bottom-right-radius: ${pixelate(radius[2])};
279
+ border-bottom-left-radius: ${pixelate(radius[3])};
280
+ `;
281
+ }
282
+ return `border-radius: ${pixelate(radius)}`;
283
+ },
284
+ },
285
+ borderTopLeftRadius: {
286
+ css: 'border-top-left-radius',
287
+ format: pixelate,
288
+ },
289
+ borderTopRightRadius: {
290
+ css: 'border-top-right-radius',
291
+ format: pixelate,
292
+ },
293
+ borderBottomRightRadius: {
294
+ css: 'border-bottom-right-radius',
295
+ format: pixelate,
296
+ },
297
+ borderBottomLeftRadius: {
298
+ css: 'border-bottom-left-radius',
299
+ format: pixelate,
300
+ },
301
+ borderColor: {
302
+ css: 'border-color',
303
+ withTheme: true,
304
+ },
305
+ borderImage: {
306
+ css: 'border-image',
307
+ withTheme: true,
308
+ },
309
+ ...[
310
+ ['border', 'border'],
311
+ ['borderTop', 'border-top'],
312
+ ['borderRight', 'border-right'],
313
+ ['borderBottom', 'border-bottom'],
314
+ ['borderLeft', 'border-left'],
315
+ ].reduce(
316
+ (rules, [jsKey, cssKey]) => ({
317
+ ...rules,
318
+ [jsKey]: {
319
+ css: (props, media, extensions) => {
320
+ const propValue = getPropValue(
321
+ props,
322
+ media,
323
+ extensions?.length ? [jsKey, ...extensions].join('$') : jsKey,
324
+ );
325
+
326
+ // withTheme
327
+ const value =
328
+ typeof propValue === 'function'
329
+ ? propValue(props.theme)
330
+ : propValue;
331
+
332
+ if (!/^\d/.test(value)) {
333
+ return `${cssKey}: 1px solid ${value}`;
334
+ }
335
+ return `${cssKey}: ${value}`;
336
+ },
337
+ },
338
+ }),
339
+ {},
340
+ ),
341
+ transition: (props, media) => {
342
+ const value = getPropValue(props, media, 'transition');
343
+ return typeof value === 'string'
344
+ ? `transition: ${value}`
345
+ : 'transition: all .2s';
346
+ },
347
+ 'boxShadow|shadow': {
348
+ css: 'box-shadow',
349
+ withTheme: true,
350
+ },
351
+ textShadow: {
352
+ css: 'text-shadow',
353
+ withTheme: true,
354
+ },
355
+ color: {
356
+ css: 'color',
357
+ withTheme: true,
358
+ },
359
+ fill: {
360
+ css: 'fill',
361
+ withTheme: true,
362
+ },
363
+ fontSize: {
364
+ css: 'font-size',
365
+ format: pixelate,
366
+ },
367
+ outline: {
368
+ css: 'outline',
369
+ withTheme: true,
370
+ },
371
+ };
package/src/utils.js ADDED
@@ -0,0 +1,83 @@
1
+ const { entries } = Object;
2
+
3
+ export const upperFirst = str => str.charAt(0).toUpperCase() + str.slice(1);
4
+
5
+ export const kebabCase = (string = '') =>
6
+ string
7
+ .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
8
+ ?.map(x => x.toLowerCase())
9
+ ?.join('-') ?? '';
10
+
11
+ export function spacing(val, theme) {
12
+ if (/(px|%)$/.test(val)) {
13
+ return val;
14
+ }
15
+
16
+ if (typeof val === 'string') {
17
+ return val;
18
+ }
19
+
20
+ if (val === 0) {
21
+ return val;
22
+ }
23
+
24
+ return theme.spacing(val);
25
+ }
26
+
27
+ export function pixelate(val) {
28
+ if (typeof val === 'number') {
29
+ return `${val}px`;
30
+ }
31
+ return val;
32
+ }
33
+
34
+ export function getPropValue(props, media, ...propKeys) {
35
+ let value;
36
+ for (let i = 0; i < propKeys.length; i++) {
37
+ const key = propKeys[i];
38
+ if (media) {
39
+ const mediaVal = props[`$${media}$${key}`];
40
+ if (mediaVal) {
41
+ value = mediaVal;
42
+ break;
43
+ }
44
+ }
45
+ const val = props[`$${key}`];
46
+ if (val) {
47
+ value = val;
48
+ break;
49
+ }
50
+ }
51
+
52
+ return value;
53
+ }
54
+
55
+ export function isPropPresent(props, media, ...propKeys) {
56
+ return !!getPropValue(props, media, ...propKeys);
57
+ }
58
+
59
+ let ruleIndex = null;
60
+ export function getRuleIndex(rules) {
61
+ if (ruleIndex) return ruleIndex;
62
+
63
+ const index = {};
64
+ entries(rules).forEach(([ruleProp, config]) => {
65
+ ruleProp.split('|').forEach(prop => {
66
+ index[prop] = config;
67
+ });
68
+ });
69
+ ruleIndex = index;
70
+
71
+ return index;
72
+ }
73
+
74
+ let mediaPropRegex = null;
75
+ export function getMediaPropRegex(theme) {
76
+ if (mediaPropRegex) return mediaPropRegex;
77
+
78
+ mediaPropRegex = new RegExp(
79
+ `^\\$(${theme.Breakpoints.keys.join('|')})(\\$.*)`,
80
+ );
81
+
82
+ return mediaPropRegex;
83
+ }