@vitus-labs/elements 0.73.0 → 0.75.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.
@@ -1,24 +1,49 @@
1
1
  import { makeItResponsive, alignContent, extendCss, value } from '@vitus-labs/unistyle';
2
2
  export { Provider } from '@vitus-labs/unistyle';
3
3
  import React, { forwardRef, memo, useMemo, createRef, useCallback, Children, useState, useEffect, createContext, useContext, useRef } from 'react';
4
- import { config, render, get, isEmpty, pick, omit, context as context$1 } from '@vitus-labs/core';
4
+ import { config, render, get, isEmpty, pick, omit, context as context$1, throttle } from '@vitus-labs/core';
5
5
  import { isFragment } from 'react-is';
6
6
  import { createPortal } from 'react-dom';
7
7
 
8
8
  // eslint-disable-next-line import/prefer-default-export
9
9
  const PKG_NAME = '@vitus-labs/elements';
10
10
 
11
+ /* eslint-disable import/prefer-default-export */
12
+ // https://stackoverflow.com/questions/35464067/flexbox-not-working-on-button-or-fieldset-elements
13
+ const INLINE_ELEMENTS_FLEX_FIX = {
14
+ button: true,
15
+ fieldset: true,
16
+ legend: true,
17
+ };
18
+
19
+ /* eslint-disable import/prefer-default-export */
20
+ const isWebFixNeeded = (tag) => {
21
+ if (tag && tag in INLINE_ELEMENTS_FLEX_FIX)
22
+ return true;
23
+ return false;
24
+ };
25
+
11
26
  const childFix = `
12
27
  display: flex;
13
28
  flex: 1;
14
29
  width: 100%;
15
30
  height: 100%;
16
31
  `;
32
+ const parentFix = `
33
+ flex-direction: column;
34
+ `;
35
+ const parentFixBlock = `
36
+ width: 100%;
37
+ `;
38
+ const fullHeight = `
39
+ height: 100%;
40
+ `;
17
41
  const block = `
18
42
  align-self: stretch;
19
43
  `;
44
+ const childFixPosition = (isBlock) => `display: ${isBlock ? 'flex' : 'inline-flex'};`;
20
45
  const styles$2 = ({ theme: t, css }) => css `
21
- ${false };
46
+ ${t.alignY === 'block' && fullHeight};
22
47
 
23
48
  ${alignContent({
24
49
  direction: t.direction,
@@ -28,13 +53,13 @@ const styles$2 = ({ theme: t, css }) => css `
28
53
 
29
54
  ${t.block && block};
30
55
 
31
- ${false };
32
- ${false };
33
- ${false };
56
+ ${!t.childFix && childFixPosition(t.block)};
57
+ ${t.parentFix && t.block && parentFixBlock};
58
+ ${t.parentFix && parentFix};
34
59
 
35
60
  ${t.extraStyles && extendCss(t.extraStyles)};
36
61
  `;
37
- const platformStyles$1 = `display: flex;`;
62
+ const platformStyles$1 = `box-sizing: border-box;` ;
38
63
  var Styled$2 = config.styled(config.component) `
39
64
  position: relative;
40
65
  ${platformStyles$1};
@@ -61,7 +86,9 @@ const component$4 = forwardRef(({ children, tag, block, extendCss, direction, al
61
86
  ref,
62
87
  as: tag,
63
88
  };
64
- {
89
+ const needsFix = !props.dangerouslySetInnerHTML && tag && isWebFixNeeded(tag)
90
+ ;
91
+ if (!needsFix || false) {
65
92
  return (React.createElement(Styled$2, { ...COMMON_PROPS, "$element": {
66
93
  block,
67
94
  direction,
@@ -71,6 +98,20 @@ const component$4 = forwardRef(({ children, tag, block, extendCss, direction, al
71
98
  extraStyles: extendCss,
72
99
  } }, children));
73
100
  }
101
+ // eslint-disable-next-line no-nested-ternary
102
+ const asTag = (isInline ? 'span' : 'div') ;
103
+ return (React.createElement(Styled$2, { ...COMMON_PROPS, "$element": {
104
+ parentFix: true,
105
+ block,
106
+ extraStyles: extendCss,
107
+ } },
108
+ React.createElement(Styled$2, { as: asTag, "$childFix": true, "$element": {
109
+ childFix: true,
110
+ direction,
111
+ alignX,
112
+ alignY,
113
+ equalCols,
114
+ } }, children)));
74
115
  });
75
116
 
76
117
  const equalCols = `
@@ -120,7 +161,7 @@ const styles$1 = ({ css, theme: t, rootSize }) => css `
120
161
 
121
162
  ${t.extraStyles && extendCss(t.extraStyles)};
122
163
  `;
123
- const platformStyles = '';
164
+ const platformStyles = `box-sizing: border-box;` ;
124
165
  var Styled$1 = config.styled(config.component) `
125
166
  ${platformStyles};
126
167
 
@@ -157,6 +198,68 @@ const Component$6 = ({ contentType, tag, parentDirection, direction, alignX, ali
157
198
  };
158
199
  var component$3 = memo(Component$6);
159
200
 
201
+ const INLINE_ELEMENTS = {
202
+ span: true,
203
+ a: true,
204
+ button: true,
205
+ input: true,
206
+ label: true,
207
+ select: true,
208
+ textarea: true,
209
+ br: true,
210
+ img: true,
211
+ strong: true,
212
+ small: true,
213
+ code: true,
214
+ b: true,
215
+ big: true,
216
+ i: true,
217
+ tt: true,
218
+ abbr: true,
219
+ acronym: true,
220
+ cite: true,
221
+ dfn: true,
222
+ em: true,
223
+ kbd: true,
224
+ samp: true,
225
+ var: true,
226
+ bdo: true,
227
+ map: true,
228
+ object: true,
229
+ q: true,
230
+ script: true,
231
+ sub: true,
232
+ sup: true,
233
+ };
234
+ const EMPTY_ELEMENTS = {
235
+ area: true,
236
+ base: true,
237
+ br: true,
238
+ col: true,
239
+ embed: true,
240
+ hr: true,
241
+ img: true,
242
+ input: true,
243
+ keygen: true,
244
+ link: true,
245
+ // 'meta': true,
246
+ // 'param': true,
247
+ source: true,
248
+ track: true,
249
+ wbr: true,
250
+ };
251
+
252
+ const isInlineElement = (tag) => {
253
+ if (tag && tag in INLINE_ELEMENTS)
254
+ return true;
255
+ return false;
256
+ };
257
+ const getShouldBeEmpty = (tag) => {
258
+ if (tag && tag in EMPTY_ELEMENTS)
259
+ return true;
260
+ return false;
261
+ };
262
+
160
263
  const defaultDirection = 'inline';
161
264
  const defaultContentDirection = 'rows';
162
265
  const defaultAlignX = 'left';
@@ -166,14 +269,14 @@ const Component$5 = forwardRef(({ innerRef, tag, label, content, children, befor
166
269
  // check if should render only single element
167
270
  // --------------------------------------------------------
168
271
  const shouldBeEmpty = !!props.dangerouslySetInnerHTML ||
169
- (false );
272
+ (tag && getShouldBeEmpty(tag));
170
273
  // --------------------------------------------------------
171
274
  // if not single element, calculate values
172
275
  // --------------------------------------------------------
173
276
  const isSimpleElement = !beforeContent && !afterContent;
174
277
  const CHILDREN = children || content || label;
175
- const isInline = false;
176
- const SUB_TAG = undefined;
278
+ const isInline = isInlineElement(tag) ;
279
+ const SUB_TAG = isInline ? 'span' : undefined;
177
280
  // --------------------------------------------------------
178
281
  // direction & alignX & alignY calculations
179
282
  // --------------------------------------------------------
@@ -630,7 +733,7 @@ position = 'fixed', // absolute | fixed | relative | static
630
733
  align = 'bottom', // * main align prop * top | left | bottom | right
631
734
  alignX = 'left', // left | center | right
632
735
  alignY = 'bottom', // top | center | bottom
633
- offsetX = 0, offsetY = 0, throttleDelay = 200, parentContainer, closeOnEsc = true, disabled, onOpen, onClose, }) => {
736
+ offsetX = 0, offsetY = 0, throttleDelay = 200, parentContainer, closeOnEsc = true, disabled, onOpen, onClose, } = {}) => {
634
737
  const { rootSize } = useContext(context$1);
635
738
  const ctx = useOverlayContext();
636
739
  const [isContentLoaded, setContentLoaded] = useState(false);
@@ -808,22 +911,29 @@ offsetX = 0, offsetY = 0, throttleDelay = 200, parentContainer, closeOnEsc = tru
808
911
  const assignContentPosition = useCallback((values = {}) => {
809
912
  if (!contentRef.current)
810
913
  return;
914
+ const isValue = (value) => {
915
+ if (typeof value === 'number')
916
+ return true;
917
+ if (Number.isFinite(value))
918
+ return true;
919
+ return !!value;
920
+ };
811
921
  const setValue = (param) => value(param, rootSize);
812
922
  // ADD POSITION STYLES TO CONTENT
813
923
  // eslint-disable-next-line no-param-reassign
814
- if (position)
924
+ if (isValue(position))
815
925
  contentRef.current.style.position = position;
816
926
  // eslint-disable-next-line no-param-reassign
817
- if (values.top)
927
+ if (isValue(values.top))
818
928
  contentRef.current.style.top = setValue(values.top);
819
929
  // eslint-disable-next-line no-param-reassign
820
- if (values.bottom)
930
+ if (isValue(values.bottom))
821
931
  contentRef.current.style.bottom = setValue(values.bottom);
822
932
  // eslint-disable-next-line no-param-reassign
823
- if (values.left)
933
+ if (isValue(values.left))
824
934
  contentRef.current.style.left = setValue(values.left);
825
935
  // eslint-disable-next-line no-param-reassign
826
- if (values.right)
936
+ if (isValue(values.right))
827
937
  contentRef.current.style.right = setValue(values.right);
828
938
  }, [position, rootSize, contentRef]);
829
939
  const setContentPosition = useCallback(() => {
@@ -886,9 +996,23 @@ offsetX = 0, offsetY = 0, throttleDelay = 200, parentContainer, closeOnEsc = tru
886
996
  triggerRef,
887
997
  contentRef,
888
998
  ]);
889
- const handleContentPosition = setContentPosition;
999
+ const handleContentPosition = useCallback(throttle(setContentPosition, throttleDelay),
1000
+ // same deps as `setContentPosition`
1001
+ [assignContentPosition, calculateContentPosition]);
890
1002
  const handleClick = handleVisibilityByEventType;
891
- const handleVisibility = handleVisibilityByEventType;
1003
+ const handleVisibility = useCallback(throttle(handleVisibilityByEventType, throttleDelay),
1004
+ // same deps as `handleVisibilityByEventType`
1005
+ [
1006
+ active,
1007
+ blocked,
1008
+ disabled,
1009
+ openOn,
1010
+ closeOn,
1011
+ hideContent,
1012
+ showContent,
1013
+ triggerRef,
1014
+ contentRef,
1015
+ ]);
892
1016
  // --------------------------------------------------------------------------
893
1017
  // useEffects
894
1018
  // --------------------------------------------------------------------------
@@ -900,10 +1024,10 @@ offsetX = 0, offsetY = 0, throttleDelay = 200, parentContainer, closeOnEsc = tru
900
1024
  }
901
1025
  }, [disabled, alignX, alignY, hideContent]);
902
1026
  useEffect(() => {
903
- if (active && isContentLoaded) {
904
- setContentPosition();
905
- setContentPosition();
906
- }
1027
+ if (!active || !isContentLoaded)
1028
+ return undefined;
1029
+ setContentPosition();
1030
+ setContentPosition();
907
1031
  }, [active, isContentLoaded, setContentPosition]);
908
1032
  // if an Overlay has an Overlay child, this will prevent closing parent child
909
1033
  // and calculates correct position when an Overlay is opened
@@ -914,12 +1038,14 @@ offsetX = 0, offsetY = 0, throttleDelay = 200, parentContainer, closeOnEsc = tru
914
1038
  if (ctx.setBlocked)
915
1039
  ctx.setBlocked();
916
1040
  }
1041
+ else {
1042
+ setContentLoaded(false);
1043
+ }
917
1044
  return () => {
918
1045
  if (onClose)
919
1046
  onClose();
920
1047
  if (ctx.setUnblocked)
921
1048
  ctx.setUnblocked();
922
- setContentLoaded(false);
923
1049
  };
924
1050
  }, [active, onOpen, onClose, showContent, ctx]);
925
1051
  // handle closing only when content is active
@@ -1069,6 +1195,13 @@ var Styled = config.styled(config.textComponent) `
1069
1195
  const component = forwardRef(({ paragraph, label, children, tag, extendCss, ...props }, ref) => {
1070
1196
  const renderContent = (as = undefined) => (React.createElement(Styled, { ref: ref, as: as, "$text": { extraStyles: extendCss }, ...props }, children || label));
1071
1197
  let finalTag;
1198
+ {
1199
+ if (paragraph)
1200
+ finalTag = 'p';
1201
+ else {
1202
+ finalTag = tag;
1203
+ }
1204
+ }
1072
1205
  return renderContent(finalTag);
1073
1206
  });
1074
1207
  const name$1 = `${PKG_NAME}/Text`;