@zuzjs/ui 0.3.2 → 0.3.4

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.
Files changed (88) hide show
  1. package/README.md +0 -0
  2. package/dist/hooks.js +89 -0
  3. package/dist/styles.css +37 -62
  4. package/dist/ui.js +665 -0
  5. package/jest.config.js +0 -0
  6. package/package.json +16 -18
  7. package/rollup.config.js +30 -47
  8. package/src/comps/box.tsx +24 -28
  9. package/src/comps/button.tsx +23 -47
  10. package/src/comps/crumb.tsx +9 -0
  11. package/src/comps/form.tsx +57 -88
  12. package/src/comps/heading.tsx +25 -31
  13. package/src/comps/icon.tsx +24 -36
  14. package/src/comps/input.tsx +24 -224
  15. package/src/comps/select.tsx +23 -63
  16. package/src/comps/spinner.tsx +23 -35
  17. package/src/comps/stylesheet.tsx +5 -0
  18. package/src/context/AppContext.tsx +2 -2
  19. package/src/context/AppProvider.tsx +68 -105
  20. package/src/context/createSlice.tsx +15 -39
  21. package/src/context/index.tsx +4 -5
  22. package/src/core/css.ts +1 -0
  23. package/src/core/index.tsx +241 -0
  24. package/src/core/styles.ts +378 -371
  25. package/src/hooks/index.tsx +2 -10
  26. package/src/hooks/useDispatch.tsx +36 -36
  27. package/src/hooks/useStore.tsx +24 -26
  28. package/src/hooks.tsx +8 -0
  29. package/src/scss/mixins.scss +2 -2
  30. package/src/scss/props.scss +91 -69
  31. package/src/scss/{style.scss → styles.scss} +102 -132
  32. package/src/ui.tsx +13 -0
  33. package/tsconfig.json +0 -0
  34. package/tsconfig.lib.json +0 -0
  35. package/tsconfig.spec.json +0 -0
  36. package/dist/index.js +0 -1868
  37. package/src/actions/addForm.tsx +0 -0
  38. package/src/actions/index.tsx +0 -29
  39. package/src/actions/redo.tsx +0 -1
  40. package/src/actions/reset.tsx +0 -1
  41. package/src/actions/undo.tsx +0 -1
  42. package/src/comps/app.tsx +0 -34
  43. package/src/comps/checkbox.tsx +0 -74
  44. package/src/comps/component.tsx +0 -32
  45. package/src/comps/contextmenu.tsx +0 -60
  46. package/src/comps/cover.tsx +0 -34
  47. package/src/comps/image.tsx +0 -34
  48. package/src/comps/masonry.tsx +0 -192
  49. package/src/comps/mediaplayer.tsx +0 -12
  50. package/src/comps/placeholder.tsx +0 -58
  51. package/src/comps/root.tsx +0 -32
  52. package/src/comps/spacer.tsx +0 -20
  53. package/src/comps/text.tsx +0 -27
  54. package/src/comps/toaster.tsx +0 -117
  55. package/src/comps/tweet.tsx +0 -48
  56. package/src/context/_AppProvider.tsx +0 -116
  57. package/src/context/combineReducers.tsx +0 -47
  58. package/src/context/combineState.tsx +0 -14
  59. package/src/context/reduceReducers.tsx +0 -6
  60. package/src/context/store/appbase.tsx +0 -19
  61. package/src/context/store/lang.tsx +0 -26
  62. package/src/context/store/theme.tsx +0 -54
  63. package/src/core/defaultTheme.ts +0 -90
  64. package/src/core/extractCurrentDesignState.tsx +0 -0
  65. package/src/core/index.ts +0 -431
  66. package/src/core/router.ts +0 -86
  67. package/src/hooks/useAppReducer.tsx +0 -40
  68. package/src/hooks/useChooseEffect.tsx +0 -6
  69. package/src/hooks/useContextMenu.tsx +0 -123
  70. package/src/hooks/useDevice.tsx +0 -164
  71. package/src/hooks/useImage.tsx +0 -84
  72. package/src/hooks/useLang.tsx +0 -9
  73. package/src/hooks/useMediaPlayer.tsx +0 -27
  74. package/src/hooks/useNavigator.tsx +0 -6
  75. package/src/hooks/useRender.tsx +0 -29
  76. package/src/hooks/useResizeObserver.tsx +0 -84
  77. package/src/hooks/useRouter.tsx +0 -45
  78. package/src/hooks/useSelector.tsx +0 -9
  79. package/src/hooks/useTheme.tsx +0 -9
  80. package/src/hooks/useToast.tsx +0 -11
  81. package/src/index.tsx +0 -35
  82. package/src/kit/Builder.tsx +0 -18
  83. package/src/kit/Component.tsx +0 -32
  84. package/src/kit/Header.tsx +0 -21
  85. package/src/redux/slices/app.js +0 -26
  86. package/src/redux/slices/form.js +0 -46
  87. package/src/redux/store.js +0 -33
  88. package/src/scss/constants.scss +0 -4
@@ -0,0 +1,241 @@
1
+ import Hashids from "hashids"
2
+ import { nanoid } from "nanoid"
3
+ import StyleCache from './css'
4
+ import Cookies from 'js-cookie'
5
+ import axios from 'axios'
6
+ import {
7
+ cssColors,
8
+ cssProps, cssPropsDirect, cssPropsIgnore, cssPropsVals
9
+ } from "./styles"
10
+
11
+ const _Hashids = new Hashids('', 4)
12
+ const _Alphabets = "#abcdefghijklmnopqrstuvwxyz0123456789"
13
+
14
+ export const setCookie = (key : string, value : any, expiry? : number, host? : string) => Cookies.set(key, value, { expires: expiry || 7, domain: host || window.location.host })
15
+
16
+ export const getCookie = (key : string) => key == `` ? Cookies.get() : Cookies.get(key) || null;
17
+
18
+ export const removeCookie = (key : string) => Cookies.remove(key)
19
+
20
+ export const uuid = (len=11) => nanoid(len)
21
+
22
+ export const makeCSSValue = (k: any, v: any, o: any) => {
23
+ let ignore = cssPropsIgnore.indexOf(o) == -1
24
+ if(k in cssPropsDirect && ignore == true){
25
+ return cssPropsDirect[k].indexOf(`__VALUE__`) > - 1 ?
26
+ cssPropsDirect[k].replaceAll(`__VALUE__`, `${v}${"number" == typeof v ? `px` : ``}`)
27
+ : cssPropsDirect[k];
28
+ }
29
+ let unit = "number" == typeof v && ignore ? `px` : ``;
30
+ if(cssColors.indexOf(v) > -1){
31
+ v = `var(--colors-${v.replaceAll(`.`, `-`)})`;
32
+ unit = ``;
33
+ }else if(v in cssPropsVals){
34
+ v = cssPropsVals[v];
35
+ }
36
+ return `${k}:${v}${unit};`;
37
+ }
38
+
39
+ export const cleanProps = (props: {}, del=true) : {} => {
40
+ const ignore = [`as`, `hover`, `bref`, `tag`,`onSubmit`,`method`,`should`,`options`,`required`]
41
+ let items = { ...props }
42
+ Object.keys(props).map(k => {
43
+ if(k in cssProps || ignore.indexOf(k) > -1){
44
+ if(del)
45
+ delete items[k]
46
+ else
47
+ items[k] = undefined
48
+ }
49
+ })
50
+ return items
51
+ }
52
+
53
+ export const buildCSS = (k : any, v : any) => {
54
+ if(k in cssProps){
55
+ return makeCSSValue(cssProps[k], v, k)
56
+ }
57
+ return ``
58
+ }
59
+
60
+ const _withStyle = (props, pseudo=``) : string => {
61
+ if(!props) return ``
62
+ let cx = [props.className || props.as || ``]
63
+ const propRegexp = /\#|px|\(|\)|-|\s/g
64
+ Object.keys(props).map(k => {
65
+ if(k != 'children' && k in cssProps){
66
+ const _css = buildCSS(k, props[k]).toString().replace(/;|:|\s/g, "")
67
+ let _indices = 0
68
+ for(let i = 0; i < _css.length; i++){ _indices += _Alphabets.indexOf(_css.charAt(i)) }
69
+ let _cp = k.substring(0, 1);
70
+ if(cssProps[k].indexOf("-") > -1){
71
+ _cp = "";
72
+ cssProps[k].split("-").map(c => _cp += c.substring(0, 1))
73
+ }
74
+ if(props[k].toString().indexOf("-") > -1){
75
+ props[k].toString().split("-").map(c => _cp += c.substring(0, 1))
76
+ }
77
+ const _id = `${_cp}${_Hashids.encode(cssProps[k].length + _indices + (isNaN(parseInt(props[k])) ? 0 : parseInt(props[k])))}`
78
+ // const _id = `${_cp}${cssProps[k]}-${props[k]}`
79
+ cx.push(_id)
80
+ StyleCache[_id + pseudo] = buildCSS(k, props[k])
81
+ }
82
+ })
83
+ return cx.join(" ").trim()
84
+ }
85
+
86
+ export const withStyle = ( props ) : string => {
87
+ const style = [_withStyle(props)]
88
+ if("hover" in props){
89
+ style.push(_withStyle(props.hover, `:hover`))
90
+ }
91
+ if("before" in props){
92
+ style.push(_withStyle(props.hover, `:before`))
93
+ }
94
+ if("after" in props){
95
+ style.push(_withStyle(props.hover, `:after`))
96
+ }
97
+ return style.join(style.length > 1 ? " " : "")
98
+ }
99
+
100
+ export const getStylesheet = () => Object.keys(StyleCache).map(ck => StyleCache[ck] != "" ? `.${ck}{${StyleCache[ck]}}` : `__`).filter(x => x != `__`)
101
+
102
+ export const isEmail = (e : string) => {
103
+ let reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ ;
104
+ return reg.test(e);
105
+ }
106
+
107
+ export const buildFormData = form => {
108
+ if (!form || form.nodeName !== "FORM") {
109
+ return;
110
+ }
111
+ var i, j, q = [];
112
+ for (i = form.elements.length - 1; i >= 0; i = i - 1) {
113
+ if (form.elements[i].name === "") {
114
+ continue;
115
+ }
116
+ switch (form.elements[i].nodeName) {
117
+ case 'INPUT':
118
+ switch (form.elements[i].type) {
119
+ case 'text':
120
+ case 'hidden':
121
+ case 'password':
122
+ case 'button':
123
+ case 'reset':
124
+ case 'submit':
125
+ q.push({ [form.elements[i].name] : encodeURIComponent(form.elements[i].value) });
126
+ break;
127
+ case 'checkbox':
128
+ case 'radio':
129
+ if (form.elements[i].checked) {
130
+ q.push({ [form.elements[i].name] : encodeURIComponent(form.elements[i].value) });
131
+ }
132
+ break;
133
+ case 'file':
134
+ break;
135
+ }
136
+ break;
137
+ case 'TEXTAREA':
138
+ q.push({ [form.elements[i].name] : encodeURIComponent(form.elements[i].value) });
139
+ break;
140
+ case 'SELECT':
141
+ switch (form.elements[i].type) {
142
+ case 'select-one':
143
+ q.push({ [form.elements[i].name] : encodeURIComponent(form.elements[i].value) });
144
+ break;
145
+ case 'select-multiple':
146
+ for (j = form.elements[i].options.length - 1; j >= 0; j = j - 1) {
147
+ if (form.elements[i].options[j].selected) {
148
+ q.push({ [form.elements[i].name] : encodeURIComponent(form.elements[i].options[j].value) });
149
+ }
150
+ }
151
+ break;
152
+ }
153
+ break;
154
+ case 'BUTTON':
155
+ switch (form.elements[i].type) {
156
+ case 'reset':
157
+ case 'submit':
158
+ case 'button':
159
+ q.push({ [form.elements[i].name] : encodeURIComponent(form.elements[i].value) });
160
+ break;
161
+ }
162
+ break;
163
+ }
164
+ }
165
+ return q //return q.join("&");
166
+ }
167
+
168
+ export const withRest = async (uri : string, data : object, timeout : number = 60, fd : object = null, progress? : Function, bearer : string = `__ha`) => {
169
+ var Bearer = getCookie(bearer) || `${uuid(8)}^${uuid(8)}`;
170
+ var isWindow = typeof window !== 'undefined'
171
+ var cancelToken = null
172
+ if(isWindow){
173
+ window['__grabToken'] = axios.CancelToken.source();
174
+ cancelToken = window['__grabToken'].token
175
+ }
176
+ if(fd){
177
+ return new Promise((resolve, reject) => {
178
+ axios({
179
+ method: "post",
180
+ url: uri,
181
+ data: buildFormData(data),
182
+ timeout: 1000 * timeout,
183
+ cancelToken: cancelToken,
184
+ headers: {
185
+ 'Content-Type': 'multipart/form-data',
186
+ 'Authorization': `Bearer ${Bearer}`
187
+ },
188
+ onUploadProgress: ev => {
189
+ //TODO: Add progress
190
+ }
191
+ })
192
+ .then(resp => {
193
+ if(resp.data && "kind" in resp.data){
194
+ resolve(resp.data)
195
+ }else{
196
+ reject(resp.data)
197
+ }
198
+ })
199
+ .catch(err => reject(err));
200
+ })
201
+ }
202
+ return new Promise((resolve, reject) => {
203
+ axios.post(
204
+ uri,
205
+ {
206
+ ...Cookies.get(),
207
+ ...data,
208
+ __poken: new Date().getTime() / 1000
209
+ },
210
+ {
211
+ timeout: 1000 * timeout,
212
+ headers: {
213
+ 'Content-Type': 'application/json',
214
+ 'Authorization': `Bearer ${Bearer}`
215
+ },
216
+ cancelToken: cancelToken
217
+ }
218
+ )
219
+ .then(resp => {
220
+ if(resp.data && "kind" in resp.data){
221
+ resolve(resp.data)
222
+ }else{
223
+ reject(resp.data)
224
+ }
225
+ })
226
+ .catch(err => reject(err));
227
+ })
228
+ }
229
+
230
+ export const formatBytes = bytes => {
231
+ var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
232
+ if (bytes == 0) return '0 Byte';
233
+ var i = Math.floor(Math.log(bytes) / Math.log(1024)),
234
+ nx = bytes / Math.pow(1024, i);
235
+ return nx.toFixed(2) + ' ' + sizes[i];
236
+ }
237
+
238
+ export const formatSeconds = (n: number): string => {
239
+ let d = new Date(n * 1000).toISOString().slice(11, 19)
240
+ return d.indexOf("00:") > -1 ? d.replace("00:", "") : d
241
+ }