@tecsinapse/react-core 1.12.5 → 1.12.6

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/CHANGELOG.md CHANGED
@@ -3,6 +3,14 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [1.12.6](https://github.com/tecsinapse/design-system/compare/@tecsinapse/react-core@1.12.5...@tecsinapse/react-core@1.12.6) (2022-01-06)
7
+
8
+ **Note:** Version bump only for package @tecsinapse/react-core
9
+
10
+
11
+
12
+
13
+
6
14
  ## [1.12.5](https://github.com/tecsinapse/design-system/compare/@tecsinapse/react-core@1.12.4...@tecsinapse/react-core@1.12.5) (2022-01-05)
7
15
 
8
16
  **Note:** Version bump only for package @tecsinapse/react-core
@@ -11,13 +11,13 @@ var _react2 = _interopRequireWildcard(require("react"));
11
11
 
12
12
  var _reactNative = require("react-native");
13
13
 
14
- var _PressableSurface = require("../PressableSurface");
14
+ var _utils = require("../../../utils");
15
15
 
16
- var _styled = require("./styled");
16
+ var _PressableSurface = require("../PressableSurface");
17
17
 
18
18
  var _animation = require("./animation");
19
19
 
20
- var _utils = require("../../../utils");
20
+ var _styled = require("./styled");
21
21
 
22
22
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
23
23
 
@@ -38,42 +38,50 @@ const Switch = ({
38
38
  }) => {
39
39
  const theme = (0, _react.useTheme)();
40
40
 
41
- const transitionValue = _react2.default.useRef(new _reactNative.Animated.Value(active ? 16.5 : 0)).current;
41
+ const width = _react2.default.useRef(0);
42
+
43
+ const transitionValue = _react2.default.useRef(new _reactNative.Animated.Value(0)).current;
42
44
 
43
- const animatedColor = _react2.default.useRef(new _reactNative.Animated.Value(active ? 1 : 0)).current;
45
+ const animatedColor = _react2.default.useRef(new _reactNative.Animated.Value(0)).current;
44
46
 
45
- const getBackgroundColor = (color, variation) => {
46
- return disabled ? (0, _utils.lightenDarkenColor)(color, variation) : color;
47
+ const calculateTranslate = () => {
48
+ if (width.current > 0) {
49
+ return width.current - (0, _utils.extractNumbersFromString)(theme.spacing.micro) * 2 - (0, _utils.extractNumbersFromString)((0, _utils.RFValueStr)(_styled.SWITCH_PIN_WIDTH));
50
+ }
51
+
52
+ return 0;
53
+ };
54
+
55
+ const getBackgroundColor = color => {
56
+ return disabled ? (0, _utils.lightenDarkenColor)(theme.color[inactiveColor][inactiveColorTone], 20) : color;
47
57
  };
48
58
 
49
59
  const interpolateColor = animatedColor.interpolate({
50
60
  inputRange: [0, 1],
51
- outputRange: [getBackgroundColor(theme.color[inactiveColor][inactiveColorTone], 25), getBackgroundColor(theme.color[activeColor][activeColorTone], 50)]
61
+ outputRange: [getBackgroundColor(theme.color[inactiveColor][inactiveColorTone]), getBackgroundColor(theme.color[activeColor][activeColorTone])]
52
62
  });
53
- const animatedStyle = {
54
- backgroundColor: interpolateColor
55
- };
56
63
  (0, _react2.useEffect)(() => {
57
- (0, _animation.transitionSwitch)(!active, transitionValue, animatedColor);
64
+ const translate = calculateTranslate();
65
+ (0, _animation.transitionSwitch)(active, translate, transitionValue, animatedColor);
58
66
  }, [active]);
59
67
  const handleChange = (0, _react2.useCallback)(() => {
60
68
  onChange(!active);
61
69
  }, [active, onChange]);
62
- const stylesDefault = {
63
- borderRadius: (0, _utils.extractNumbersFromString)(theme.borderRadius.pill),
64
- paddingHorizontal: (0, _utils.extractNumbersFromString)(theme.spacing.micro),
65
- paddingVertical: 0,
66
- justifyContent: 'center',
67
- width: 40,
68
- height: 22
70
+
71
+ const handleSwitchLayout = lce => {
72
+ width.current = lce.nativeEvent.layout.width;
73
+ const translate = calculateTranslate();
74
+ (0, _animation.transitionSwitch)(active, translate, transitionValue, animatedColor);
69
75
  };
76
+
70
77
  return _react2.default.createElement(_PressableSurface.PressableSurface, _extends({}, rest, {
71
78
  onPress: handleChange,
72
79
  effect: "none",
73
80
  disabled: disabled
74
- }), _react2.default.createElement(_reactNative.Animated.View, {
75
- style: { ...animatedStyle,
76
- ...stylesDefault
81
+ }), _react2.default.createElement(_styled.StyledSwitchContent, {
82
+ onLayout: handleSwitchLayout,
83
+ style: {
84
+ backgroundColor: interpolateColor
77
85
  }
78
86
  }, _react2.default.createElement(_styled.StyledSwitch, {
79
87
  style: [dotStyle, {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/components/atoms/Switch/Switch.tsx"],"names":["Switch","onChange","activeColor","activeColorTone","inactiveColor","inactiveColorTone","active","dotStyle","disabled","rest","theme","transitionValue","React","useRef","Animated","Value","current","animatedColor","getBackgroundColor","color","variation","interpolateColor","interpolate","inputRange","outputRange","animatedStyle","backgroundColor","handleChange","stylesDefault","borderRadius","pill","paddingHorizontal","spacing","micro","paddingVertical","justifyContent","width","height","transform","translateX"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AAMA;;AACA;;AACA;;AACA;;;;;;;;AAcA,MAAMA,MAAuB,GAAG,CAAC;AAC/BC,EAAAA,QAD+B;AAE/BC,EAAAA,WAAW,GAAG,SAFiB;AAG/BC,EAAAA,eAAe,GAAG,QAHa;AAI/BC,EAAAA,aAAa,GAAG,WAJe;AAK/BC,EAAAA,iBAAiB,GAAG,OALW;AAM/BC,EAAAA,MAN+B;AAO/BC,EAAAA,QAP+B;AAQ/BC,EAAAA,QAAQ,GAAG,KARoB;AAS/B,KAAGC;AAT4B,CAAD,KAUb;AACjB,QAAMC,KAAK,GAAG,sBAAd;;AAEA,QAAMC,eAAe,GAAGC,gBAAMC,MAAN,CAAa,IAAIC,sBAASC,KAAb,CAAmBT,MAAM,GAAG,IAAH,GAAU,CAAnC,CAAb,EACrBU,OADH;;AAGA,QAAMC,aAAa,GAAGL,gBAAMC,MAAN,CAAa,IAAIC,sBAASC,KAAb,CA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,kCAAD,eACM5B,IADN;AAEE,IAAA,OAAO,EAAEkB,YAFX;AAGE,IAAA,MAAM,EAAC,MAHT;AAIE,IAAA,QAAQ,EAAEnB;AAJZ,MAME,8BAAC,qBAAD,CAAU,IAAV;AAAe,IAAA,KAAK,EAAE,EAAE,GAAGiB,aAAL;AAAoB,SAAGG;AAAvB;AAAtB,KACE,8BAAC,oBAAD;AACE,IAAA,KAAK,EAAE,CAACrB,QAAD,EAAW;AAAE+B,MAAAA,SAAS,EAAE,CAAC;AAAEC,QAAAA,UAAU,EAAE5B;AAAd,OAAD;AAAb,KAAX;AADT,IADF,CANF,CADF;AAcD,CAlED;;eAoEeX,M","sourcesContent":["import { useTheme } from '@emotion/react';\nimport React, { FC, useCallback, useEffect } from 'react';\nimport { Animated, StyleProp, ViewStyle, Text } from 'react-native';\nimport {\n ColorGradationType,\n ColorType,\n ThemeProp,\n} from '../../../types/defaults';\nimport { PressableSurface } from '../PressableSurface';\nimport { StyledSwitch } from './styled';\nimport { transitionSwitch } from './animation';\nimport { extractNumbersFromString, lightenDarkenColor } from '../../../utils';\n\nexport interface SwitchProps {\n onChange: (active: boolean) => void;\n active: boolean;\n activeColor?: ColorType;\n activeColorTone?: ColorGradationType;\n inactiveColor?: ColorType;\n inactiveColorTone?: ColorGradationType;\n style?: StyleProp<ViewStyle>;\n dotStyle?: StyleProp<ViewStyle>;\n disabled?: boolean;\n}\n\nconst Switch: FC<SwitchProps> = ({\n onChange,\n activeColor = 'primary',\n activeColorTone = 'medium',\n inactiveColor = 'secondary',\n inactiveColorTone = 'light',\n active,\n dotStyle,\n disabled = false,\n ...rest\n}): JSX.Element => {\n const theme = useTheme() as ThemeProp;\n\n const transitionValue = React.useRef(new Animated.Value(active ? 16.5 : 0))\n .current;\n\n const animatedColor = React.useRef(new Animated.Value(active ? 1 : 0))\n .current;\n\n const getBackgroundColor = (color: string, variation: number) => {\n return disabled ? lightenDarkenColor(color, variation) : color;\n };\n\n const interpolateColor = animatedColor.interpolate({\n inputRange: [0, 1],\n outputRange: [\n getBackgroundColor(theme.color[inactiveColor][inactiveColorTone], 25),\n getBackgroundColor(theme.color[activeColor][activeColorTone], 50),\n ],\n });\n\n const animatedStyle = {\n backgroundColor: interpolateColor,\n };\n\n useEffect(() => {\n transitionSwitch(!active, transitionValue, animatedColor);\n }, [active]);\n\n const handleChange = useCallback(() => {\n onChange(!active);\n }, [active, onChange]);\n\n const stylesDefault: ViewStyle = {\n borderRadius: extractNumbersFromString(theme.borderRadius.pill),\n paddingHorizontal: extractNumbersFromString(theme.spacing.micro),\n paddingVertical: 0,\n justifyContent: 'center',\n width: 40,\n height: 22,\n };\n\n return (\n <PressableSurface\n {...rest}\n onPress={handleChange}\n effect=\"none\"\n disabled={disabled}\n >\n <Animated.View style={{ ...animatedStyle, ...stylesDefault }}>\n <StyledSwitch\n style={[dotStyle, { transform: [{ translateX: transitionValue }] }]}\n />\n </Animated.View>\n </PressableSurface>\n );\n};\n\nexport default Switch;\n"],"file":"Switch.js"}
1
+ {"version":3,"sources":["../../../../src/components/atoms/Switch/Switch.tsx"],"names":["Switch","onChange","activeColor","activeColorTone","inactiveColor","inactiveColorTone","active","dotStyle","disabled","rest","theme","width","React","useRef","transitionValue","Animated","Value","current","animatedColor","calculateTranslate","spacing","micro","SWITCH_PIN_WIDTH","getBackgroundColor","color","interpolateColor","interpolate","inputRange","outputRange","translate","handleChange","handleSwitchLayout","lce","nativeEvent","layout","backgroundColor","transform","translateX"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AAMA;;AACA;;AACA;;AACA;;;;;;;;AAcA,MAAMA,MAAuB,GAAG,CAAC;AAC/BC,EAAAA,QAD+B;AAE/BC,EAAAA,WAAW,GAAG,SAFiB;AAG/BC,EAAAA,eAAe,GAAG,QAHa;AAI/BC,EAAAA,aAAa,GAAG,WAJe;AAK/BC,EAAAA,iBAAiB,GAAG,OALW;AAM/BC,EAAAA,MAN+B;AAO/BC,EAAAA,QAP+B;AAQ/BC,EAAAA,QAA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eAApC,EAAqDI,aAArD;AACD,GAHD,EAGG,CAACZ,MAAD,CAHH;AAKA,QAAMwB,YAAY,GAAG,yBAAY,MAAM;AACrC7B,IAAAA,QAAQ,CAAC,CAACK,MAAF,CAAR;AACD,GAFoB,EAElB,CAACA,MAAD,EAASL,QAAT,CAFkB,CAArB;;AAIA,QAAM8B,kBAAkB,GAAIC,GAAD,IAA4B;AACrDrB,IAAAA,KAAK,CAACM,OAAN,GAAgBe,GAAG,CAACC,WAAJ,CAAgBC,MAAhB,CAAuBvB,KAAvC;AACA,UAAMkB,SAAS,GAAGV,kBAAkB,EAApC;AACA,qCAAiBb,MAAjB,EAAyBuB,SAAzB,EAAoCf,eAApC,EAAqDI,aAArD;AACD,GAJD;;AAMA,SACE,8BAAC,kCAAD,eACMT,IADN;AAEE,IAAA,OAAO,EAAEqB,YAFX;AAGE,IAAA,MAAM,EAAC,MAHT;AAIE,IAAA,QAAQ,EAAEtB;AAJZ,MAME,8BAAC,2BAAD;AAAqB,IAAA,QAAQ,EAAEuB,kBAA/B;AAAmD,IAAA,KAAK,EAAE;AAAEI,MAAAA,eAAe,EAAEV;AAAnB;AAA1D,KACE,8BAAC,oBAAD;AAAc,IAAA,KAAK,EAAE,CAAClB,QAAD,EAAW;AAAE6B,MAAAA,SAAS,EAAE,CAAC;AAAEC,QAAAA,UAAU,EAAEvB;AAAd,OAAD;AAAb,KAAX;AAArB,IADF,CANF,CADF;AAYD,CA/DD;;eAiEed,M","sourcesContent":["import { useTheme } from '@emotion/react';\nimport React, { FC, useCallback, useEffect } from 'react';\nimport { Animated, LayoutChangeEvent, StyleProp, ViewStyle } from 'react-native';\nimport {\n ColorGradationType,\n ColorType,\n ThemeProp\n} from '../../../types/defaults';\nimport { extractNumbersFromString, lightenDarkenColor, RFValueStr } from '../../../utils';\nimport { PressableSurface } from '../PressableSurface';\nimport { transitionSwitch } from './animation';\nimport { StyledSwitch, StyledSwitchContent, SWITCH_PIN_WIDTH } from './styled';\n\nexport interface SwitchProps {\n onChange: (active: boolean) => void;\n active: boolean;\n activeColor?: ColorType;\n activeColorTone?: ColorGradationType;\n inactiveColor?: ColorType;\n inactiveColorTone?: ColorGradationType;\n style?: StyleProp<ViewStyle>;\n dotStyle?: StyleProp<ViewStyle>;\n disabled?: boolean;\n}\n\nconst Switch: FC<SwitchProps> = ({\n onChange,\n activeColor = 'primary',\n activeColorTone = 'medium',\n inactiveColor = 'secondary',\n inactiveColorTone = 'light',\n active,\n dotStyle,\n disabled = false,\n ...rest\n}): JSX.Element => {\n const theme = useTheme() as ThemeProp;\n\n const width = React.useRef(0)\n const transitionValue = React.useRef(new Animated.Value(0)).current\n const animatedColor = React.useRef(new Animated.Value(0)).current\n \n const calculateTranslate = () => {\n if (width.current > 0) {\n return width.current - (extractNumbersFromString(theme.spacing.micro) * 2) - (extractNumbersFromString(RFValueStr(SWITCH_PIN_WIDTH)))\n }\n return 0\n }\n\n const getBackgroundColor = (color: string) => {\n return disabled ? lightenDarkenColor(theme.color[inactiveColor][inactiveColorTone], 20) : color;\n };\n\n const interpolateColor = animatedColor.interpolate({\n inputRange: [0, 1],\n outputRange: [\n getBackgroundColor(theme.color[inactiveColor][inactiveColorTone]),\n getBackgroundColor(theme.color[activeColor][activeColorTone]),\n ]\n });\n\n useEffect(() => {\n const translate = calculateTranslate()\n transitionSwitch(active, translate, transitionValue, animatedColor);\n }, [active]);\n\n const handleChange = useCallback(() => {\n onChange(!active);\n }, [active, onChange]);\n\n const handleSwitchLayout = (lce: LayoutChangeEvent) => {\n width.current = lce.nativeEvent.layout.width\n const translate = calculateTranslate()\n transitionSwitch(active, translate, transitionValue, animatedColor);\n }\n\n return (\n <PressableSurface\n {...rest}\n onPress={handleChange}\n effect=\"none\"\n disabled={disabled}\n >\n <StyledSwitchContent onLayout={handleSwitchLayout} style={{ backgroundColor: interpolateColor }}>\n <StyledSwitch style={[dotStyle, { transform: [{ translateX: transitionValue }] }]}/>\n </StyledSwitchContent>\n </PressableSurface>\n );\n};\n\nexport default Switch;\n"],"file":"Switch.js"}
@@ -1,2 +1,2 @@
1
1
  import { Animated } from 'react-native';
2
- export declare const transitionSwitch: (active: boolean, transitionValue: Animated.Value, animatedColor: Animated.Value) => void;
2
+ export declare const transitionSwitch: (active: boolean, translate: number, transitionValue: Animated.Value, animatedColor: Animated.Value) => void;
@@ -7,32 +7,40 @@ exports.transitionSwitch = void 0;
7
7
 
8
8
  var _reactNative = require("react-native");
9
9
 
10
- const transitionSwitch = (active, transitionValue, animatedColor) => {
11
- if (active) {
12
- _reactNative.Animated.timing(transitionValue, {
13
- toValue: 0,
14
- duration: 150,
15
- useNativeDriver: true
16
- }).start();
10
+ const ANIMATION_SPEED = 140;
17
11
 
18
- _reactNative.Animated.timing(animatedColor, {
19
- toValue: 0,
20
- duration: 150,
21
- useNativeDriver: false
22
- }).start();
23
- } else {
12
+ const transitionSwitch = (active, translate, transitionValue, animatedColor) => {
13
+ if (translate <= 0) return;
14
+
15
+ if (active) {
24
16
  _reactNative.Animated.timing(transitionValue, {
25
- toValue: 16.5,
26
- duration: 150,
17
+ toValue: translate,
18
+ duration: ANIMATION_SPEED,
19
+ easing: _reactNative.Easing.out(_reactNative.Easing.ease),
27
20
  useNativeDriver: true
28
21
  }).start();
29
22
 
30
23
  _reactNative.Animated.timing(animatedColor, {
31
24
  toValue: 1,
32
- duration: 150,
25
+ duration: ANIMATION_SPEED,
33
26
  useNativeDriver: false
34
27
  }).start();
28
+
29
+ return;
35
30
  }
31
+
32
+ _reactNative.Animated.timing(transitionValue, {
33
+ toValue: 0,
34
+ duration: ANIMATION_SPEED,
35
+ easing: _reactNative.Easing.out(_reactNative.Easing.ease),
36
+ useNativeDriver: true
37
+ }).start();
38
+
39
+ _reactNative.Animated.timing(animatedColor, {
40
+ toValue: 0,
41
+ duration: ANIMATION_SPEED,
42
+ useNativeDriver: false
43
+ }).start();
36
44
  };
37
45
 
38
46
  exports.transitionSwitch = transitionSwitch;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/components/atoms/Switch/animation.ts"],"names":["transitionSwitch","active","transitionValue","animatedColor","Animated","timing","toValue","duration","useNativeDriver","start"],"mappings":";;;;;;;AAAA;;AAEO,MAAMA,gBAAgB,GAAG,CAC9BC,MAD8B,EAE9BC,eAF8B,EAG9BC,aAH8B,KAIrB;AACT,MAAIF,MAAJ,EAAY;AACVG,0BAASC,MAAT,CAAgBH,eAAhB,EAAiC;AAC/BI,MAAAA,OAAO,EAAE,CADsB;AAE/BC,MAAAA,QAAQ,EAAE,GAFqB;AAG/BC,MAAAA,eAAe,EAAE;AAHc,KAAjC,EAIGC,KAJH;;AAKAL,0BAASC,MAAT,CAAgBF,aAAhB,EAA+B;AAC7BG,MAAAA,OAAO,EAAE,CADoB;AAE7BC,MAAAA,QAAQ,EAAE,GAFmB;AAG7BC,MAAAA,eAAe,EAAE;AAHY,KAA/B,EAIGC,KAJH;AAKD,GAXD,MAWO;AACLL,0BAASC,MAAT,CAAgBH,eAAhB,EAAiC;AAC/BI,MAAAA,OAAO,EAAE,IADsB;AAE/BC,MAAAA,QAAQ,EAAE,GAFqB;AAG/BC,MAAAA,eAAe,EAAE;AAHc,KAAjC,EAIGC,KAJH;;AAKAL,0BAASC,MAAT,CAAgBF,aAAhB,EAA+B;AAC7BG,MAAAA,OAAO,EAAE,CADoB;AAE7BC,MAAAA,QAAQ,EAAE,GAFmB;AAG7BC,MAAAA,eAAe,EAAE;AAHY,KAA/B,EAIGC,KAJH;AAKD;AACF,CA5BM","sourcesContent":["import { Animated } from 'react-native';\n\nexport const transitionSwitch = (\n active: boolean,\n transitionValue: Animated.Value,\n animatedColor: Animated.Value\n): void => {\n if (active) {\n Animated.timing(transitionValue, {\n toValue: 0,\n duration: 150,\n useNativeDriver: true,\n }).start();\n Animated.timing(animatedColor, {\n toValue: 0,\n duration: 150,\n useNativeDriver: false,\n }).start();\n } else {\n Animated.timing(transitionValue, {\n toValue: 16.5,\n duration: 150,\n useNativeDriver: true,\n }).start();\n Animated.timing(animatedColor, {\n toValue: 1,\n duration: 150,\n useNativeDriver: false,\n }).start();\n }\n};\n"],"file":"animation.js"}
1
+ {"version":3,"sources":["../../../../src/components/atoms/Switch/animation.ts"],"names":["ANIMATION_SPEED","transitionSwitch","active","translate","transitionValue","animatedColor","Animated","timing","toValue","duration","easing","Easing","out","ease","useNativeDriver","start"],"mappings":";;;;;;;AAAA;;AAEA,MAAMA,eAAe,GAAG,GAAxB;;AAEO,MAAMC,gBAAgB,GAAG,CAC9BC,MAD8B,EAE9BC,SAF8B,EAG9BC,eAH8B,EAI9BC,aAJ8B,KAKrB;AAET,MAAIF,SAAS,IAAI,CAAjB,EAAoB;;AAEpB,MAAID,MAAJ,EAAY;AACVI,0BAASC,MAAT,CAAgBH,eAAhB,EAAiC;AAC/BI,MAAAA,OAAO,EAAEL,SADsB;AAE/BM,MAAAA,QAAQ,EAAET,eAFqB;AAG/BU,MAAAA,MAAM,EAAEC,oBAAOC,GAAP,CAAWD,oBAAOE,IAAlB,CAHuB;AAI/BC,MAAAA,eAAe,EAAE;AAJc,KAAjC,EAKGC,KALH;;AAMAT,0BAASC,MAAT,CAAgBF,aAAhB,EAA+B;AAC7BG,MAAAA,OAAO,EAAE,CADoB;AAE7BC,MAAAA,QAAQ,EAAET,eAFmB;AAG7Bc,MAAAA,eAAe,EAAE;AAHY,KAA/B,EAIGC,KAJH;;AAKA;AACD;;AAEDT,wBAASC,MAAT,CAAgBH,eAAhB,EAAiC;AAC/BI,IAAAA,OAAO,EAAE,CADsB;AAE/BC,IAAAA,QAAQ,EAAET,eAFqB;AAG/BU,IAAAA,MAAM,EAAEC,oBAAOC,GAAP,CAAWD,oBAAOE,IAAlB,CAHuB;AAI/BC,IAAAA,eAAe,EAAE;AAJc,GAAjC,EAKGC,KALH;;AAMAT,wBAASC,MAAT,CAAgBF,aAAhB,EAA+B;AAC7BG,IAAAA,OAAO,EAAE,CADoB;AAE7BC,IAAAA,QAAQ,EAAET,eAFmB;AAG7Bc,IAAAA,eAAe,EAAE;AAHY,GAA/B,EAIGC,KAJH;AAMD,CApCM","sourcesContent":["import { Animated, Easing } from 'react-native';\n\nconst ANIMATION_SPEED = 140\n\nexport const transitionSwitch = (\n active: boolean,\n translate: number,\n transitionValue: Animated.Value,\n animatedColor: Animated.Value\n): void => {\n\n if (translate <= 0) return\n \n if (active) {\n Animated.timing(transitionValue, {\n toValue: translate,\n duration: ANIMATION_SPEED,\n easing: Easing.out(Easing.ease),\n useNativeDriver: true,\n }).start();\n Animated.timing(animatedColor, {\n toValue: 1,\n duration: ANIMATION_SPEED,\n useNativeDriver: false,\n }).start();\n return\n } \n\n Animated.timing(transitionValue, {\n toValue: 0,\n duration: ANIMATION_SPEED,\n easing: Easing.out(Easing.ease),\n useNativeDriver: true,\n }).start();\n Animated.timing(animatedColor, {\n toValue: 0,\n duration: ANIMATION_SPEED,\n useNativeDriver: false,\n }).start();\n\n};\n"],"file":"animation.js"}
@@ -1,12 +1,13 @@
1
1
  /// <reference types="react" />
2
2
  import { Animated } from 'react-native';
3
- import { SwitchProps } from './Switch';
3
+ export declare const SWITCH_BODY_WIDTH = "40px";
4
+ export declare const SWITCH_PIN_WIDTH = "16px";
4
5
  export declare const StyledSwitchContent: import("@emotion/native").StyledComponent<Animated.AnimatedProps<import("react-native").ViewProps & import("react").RefAttributes<import("react-native").View>> & {
5
6
  children?: import("react").ReactNode;
6
7
  } & {
7
8
  theme?: import("@emotion/react").Theme | undefined;
8
9
  as?: import("react").ElementType<any> | undefined;
9
- } & Partial<SwitchProps> & Partial<import("../../../types/defaults").ThemeProviderProps>, {}, {}>;
10
+ } & Partial<import("../../../types/defaults").ThemeProviderProps>, {}, {}>;
10
11
  export declare const StyledSwitch: import("@emotion/native").StyledComponent<Animated.AnimatedProps<import("react-native").ViewProps & import("react").RefAttributes<import("react-native").View>> & {
11
12
  children?: import("react").ReactNode;
12
13
  } & {
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.StyledSwitch = exports.StyledSwitchContent = void 0;
6
+ exports.StyledSwitch = exports.StyledSwitchContent = exports.SWITCH_PIN_WIDTH = exports.SWITCH_BODY_WIDTH = void 0;
7
7
 
8
8
  var _native = _interopRequireDefault(require("@emotion/native"));
9
9
 
@@ -13,6 +13,10 @@ var _utils = require("../../../utils");
13
13
 
14
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
15
 
16
+ const SWITCH_BODY_WIDTH = '40px';
17
+ exports.SWITCH_BODY_WIDTH = SWITCH_BODY_WIDTH;
18
+ const SWITCH_PIN_WIDTH = '16px';
19
+ exports.SWITCH_PIN_WIDTH = SWITCH_PIN_WIDTH;
16
20
  const StyledSwitchContent = (0, _native.default)(_reactNative.Animated.View)`
17
21
  border-radius: ${({
18
22
  theme
@@ -21,7 +25,7 @@ const StyledSwitchContent = (0, _native.default)(_reactNative.Animated.View)`
21
25
  theme
22
26
  }) => theme.spacing.micro};
23
27
  justify-content: center;
24
- width: ${(0, _utils.RFValueStr)('40px')};
28
+ width: ${(0, _utils.RFValueStr)(SWITCH_BODY_WIDTH)};
25
29
  height: ${(0, _utils.RFValueStr)('22px')};
26
30
  `;
27
31
  exports.StyledSwitchContent = StyledSwitchContent;
@@ -32,7 +36,7 @@ const StyledSwitch = (0, _native.default)(_reactNative.Animated.View)`
32
36
  background-color: ${({
33
37
  theme
34
38
  }) => theme.miscellaneous.surfaceColor};
35
- width: ${(0, _utils.RFValueStr)('16px')};
39
+ width: ${(0, _utils.RFValueStr)(SWITCH_PIN_WIDTH)};
36
40
  height: ${(0, _utils.RFValueStr)('16px')};
37
41
  `;
38
42
  exports.StyledSwitch = StyledSwitch;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/components/atoms/Switch/styled.ts"],"names":["StyledSwitchContent","Animated","View","theme","borderRadius","pill","spacing","micro","StyledSwitch","miscellaneous","surfaceColor"],"mappings":";;;;;;;AAAA;;AACA;;AAEA;;;;AAGO,MAAMA,mBAAmB,GAAG,qBAAOC,sBAASC,IAAhB,CAEjC;AACF,mBAAmB,CAAC;AAAEC,EAAAA;AAAF,CAAD,KAAeA,KAAK,CAACC,YAAN,CAAmBC,IAAK;AAC1D,eAAe,CAAC;AAAEF,EAAAA;AAAF,CAAD,KAAeA,KAAK,CAACG,OAAN,CAAcC,KAAM;AAClD;AACA,WAAW,uBAAW,MAAX,CAAmB;AAC9B,YAAY,uBAAW,MAAX,CAAmB;AAC/B,CARO;;AAUA,MAAMC,YAAY,GAAG,qBAAOP,sBAASC,IAAhB,CAA2C;AACvE,mBAAmB,CAAC;AAAEC,EAAAA;AAAF,CAAD,KAAeA,KAAK,CAACC,YAAN,CAAmBC,IAAK;AAC1D,sBAAsB,CAAC;AAAEF,EAAAA;AAAF,CAAD,KAAeA,KAAK,CAACM,aAAN,CAAoBC,YAAa;AACtE,WAAW,uBAAW,MAAX,CAAmB;AAC9B,YAAY,uBAAW,MAAX,CAAmB;AAC/B,CALO","sourcesContent":["import styled from '@emotion/native';\nimport { Animated } from 'react-native';\nimport { StyleProps } from '../../../types/defaults';\nimport { RFValueStr } from '../../../utils';\nimport { SwitchProps } from './Switch';\n\nexport const StyledSwitchContent = styled(Animated.View)<\n Partial<SwitchProps> & Partial<StyleProps>\n>`\n border-radius: ${({ theme }) => theme.borderRadius.pill};\n padding: 0 ${({ theme }) => theme.spacing.micro};\n justify-content: center;\n width: ${RFValueStr('40px')};\n height: ${RFValueStr('22px')};\n`;\n\nexport const StyledSwitch = styled(Animated.View)<Partial<StyleProps>>`\n border-radius: ${({ theme }) => theme.borderRadius.pill};\n background-color: ${({ theme }) => theme.miscellaneous.surfaceColor};\n width: ${RFValueStr('16px')};\n height: ${RFValueStr('16px')};\n`;\n"],"file":"styled.js"}
1
+ {"version":3,"sources":["../../../../src/components/atoms/Switch/styled.ts"],"names":["SWITCH_BODY_WIDTH","SWITCH_PIN_WIDTH","StyledSwitchContent","Animated","View","theme","borderRadius","pill","spacing","micro","StyledSwitch","miscellaneous","surfaceColor"],"mappings":";;;;;;;AAAA;;AACA;;AAEA;;;;AAEO,MAAMA,iBAAiB,GAAG,MAA1B;;AACA,MAAMC,gBAAgB,GAAG,MAAzB;;AAEA,MAAMC,mBAAmB,GAAG,qBAAOC,sBAASC,IAAhB,CAEjC;AACF,mBAAmB,CAAC;AAAEC,EAAAA;AAAF,CAAD,KAAeA,KAAK,CAACC,YAAN,CAAmBC,IAAK;AAC1D,eAAe,CAAC;AAAEF,EAAAA;AAAF,CAAD,KAAeA,KAAK,CAACG,OAAN,CAAcC,KAAM;AAClD;AACA,WAAW,uBAAWT,iBAAX,CAA8B;AACzC,YAAY,uBAAW,MAAX,CAAmB;AAC/B,CARO;;AAUA,MAAMU,YAAY,GAAG,qBAAOP,sBAASC,IAAhB,CAA2C;AACvE,mBAAmB,CAAC;AAAEC,EAAAA;AAAF,CAAD,KAAeA,KAAK,CAACC,YAAN,CAAmBC,IAAK;AAC1D,sBAAsB,CAAC;AAAEF,EAAAA;AAAF,CAAD,KAAeA,KAAK,CAACM,aAAN,CAAoBC,YAAa;AACtE,WAAW,uBAAWX,gBAAX,CAA6B;AACxC,YAAY,uBAAW,MAAX,CAAmB;AAC/B,CALO","sourcesContent":["import styled from '@emotion/native';\nimport { Animated } from 'react-native';\nimport { StyleProps } from '../../../types/defaults';\nimport { RFValueStr } from '../../../utils';\n\nexport const SWITCH_BODY_WIDTH = '40px'\nexport const SWITCH_PIN_WIDTH = '16px'\n\nexport const StyledSwitchContent = styled(Animated.View)<\n Partial<StyleProps>\n>`\n border-radius: ${({ theme }) => theme.borderRadius.pill};\n padding: 0 ${({ theme }) => theme.spacing.micro};\n justify-content: center;\n width: ${RFValueStr(SWITCH_BODY_WIDTH)};\n height: ${RFValueStr('22px')};\n`;\n\nexport const StyledSwitch = styled(Animated.View)<Partial<StyleProps>>`\n border-radius: ${({ theme }) => theme.borderRadius.pill};\n background-color: ${({ theme }) => theme.miscellaneous.surfaceColor};\n width: ${RFValueStr(SWITCH_PIN_WIDTH)};\n height: ${RFValueStr('16px')};\n`;\n"],"file":"styled.js"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tecsinapse/react-core",
3
3
  "description": "TecSinapse hybrid React components",
4
- "version": "1.12.5",
4
+ "version": "1.12.6",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -31,5 +31,5 @@
31
31
  "react-native": ">=0.64.0",
32
32
  "react-native-vector-icons": ">=8.1.0"
33
33
  },
34
- "gitHead": "ce86b8c8b3e7c09e0fd86689418b540010f1419b"
34
+ "gitHead": "5005537c38efef79cdd69a324563821e01e81f99"
35
35
  }
@@ -1,15 +1,15 @@
1
1
  import { useTheme } from '@emotion/react';
2
2
  import React, { FC, useCallback, useEffect } from 'react';
3
- import { Animated, StyleProp, ViewStyle, Text } from 'react-native';
3
+ import { Animated, LayoutChangeEvent, StyleProp, ViewStyle } from 'react-native';
4
4
  import {
5
5
  ColorGradationType,
6
6
  ColorType,
7
- ThemeProp,
7
+ ThemeProp
8
8
  } from '../../../types/defaults';
9
+ import { extractNumbersFromString, lightenDarkenColor, RFValueStr } from '../../../utils';
9
10
  import { PressableSurface } from '../PressableSurface';
10
- import { StyledSwitch } from './styled';
11
11
  import { transitionSwitch } from './animation';
12
- import { extractNumbersFromString, lightenDarkenColor } from '../../../utils';
12
+ import { StyledSwitch, StyledSwitchContent, SWITCH_PIN_WIDTH } from './styled';
13
13
 
14
14
  export interface SwitchProps {
15
15
  onChange: (active: boolean) => void;
@@ -36,44 +36,43 @@ const Switch: FC<SwitchProps> = ({
36
36
  }): JSX.Element => {
37
37
  const theme = useTheme() as ThemeProp;
38
38
 
39
- const transitionValue = React.useRef(new Animated.Value(active ? 16.5 : 0))
40
- .current;
39
+ const width = React.useRef(0)
40
+ const transitionValue = React.useRef(new Animated.Value(0)).current
41
+ const animatedColor = React.useRef(new Animated.Value(0)).current
42
+
43
+ const calculateTranslate = () => {
44
+ if (width.current > 0) {
45
+ return width.current - (extractNumbersFromString(theme.spacing.micro) * 2) - (extractNumbersFromString(RFValueStr(SWITCH_PIN_WIDTH)))
46
+ }
47
+ return 0
48
+ }
41
49
 
42
- const animatedColor = React.useRef(new Animated.Value(active ? 1 : 0))
43
- .current;
44
-
45
- const getBackgroundColor = (color: string, variation: number) => {
46
- return disabled ? lightenDarkenColor(color, variation) : color;
50
+ const getBackgroundColor = (color: string) => {
51
+ return disabled ? lightenDarkenColor(theme.color[inactiveColor][inactiveColorTone], 20) : color;
47
52
  };
48
53
 
49
54
  const interpolateColor = animatedColor.interpolate({
50
55
  inputRange: [0, 1],
51
56
  outputRange: [
52
- getBackgroundColor(theme.color[inactiveColor][inactiveColorTone], 25),
53
- getBackgroundColor(theme.color[activeColor][activeColorTone], 50),
54
- ],
57
+ getBackgroundColor(theme.color[inactiveColor][inactiveColorTone]),
58
+ getBackgroundColor(theme.color[activeColor][activeColorTone]),
59
+ ]
55
60
  });
56
61
 
57
- const animatedStyle = {
58
- backgroundColor: interpolateColor,
59
- };
60
-
61
62
  useEffect(() => {
62
- transitionSwitch(!active, transitionValue, animatedColor);
63
+ const translate = calculateTranslate()
64
+ transitionSwitch(active, translate, transitionValue, animatedColor);
63
65
  }, [active]);
64
66
 
65
67
  const handleChange = useCallback(() => {
66
68
  onChange(!active);
67
69
  }, [active, onChange]);
68
70
 
69
- const stylesDefault: ViewStyle = {
70
- borderRadius: extractNumbersFromString(theme.borderRadius.pill),
71
- paddingHorizontal: extractNumbersFromString(theme.spacing.micro),
72
- paddingVertical: 0,
73
- justifyContent: 'center',
74
- width: 40,
75
- height: 22,
76
- };
71
+ const handleSwitchLayout = (lce: LayoutChangeEvent) => {
72
+ width.current = lce.nativeEvent.layout.width
73
+ const translate = calculateTranslate()
74
+ transitionSwitch(active, translate, transitionValue, animatedColor);
75
+ }
77
76
 
78
77
  return (
79
78
  <PressableSurface
@@ -82,11 +81,9 @@ const Switch: FC<SwitchProps> = ({
82
81
  effect="none"
83
82
  disabled={disabled}
84
83
  >
85
- <Animated.View style={{ ...animatedStyle, ...stylesDefault }}>
86
- <StyledSwitch
87
- style={[dotStyle, { transform: [{ translateX: transitionValue }] }]}
88
- />
89
- </Animated.View>
84
+ <StyledSwitchContent onLayout={handleSwitchLayout} style={{ backgroundColor: interpolateColor }}>
85
+ <StyledSwitch style={[dotStyle, { transform: [{ translateX: transitionValue }] }]}/>
86
+ </StyledSwitchContent>
90
87
  </PressableSurface>
91
88
  );
92
89
  };
@@ -1,31 +1,41 @@
1
- import { Animated } from 'react-native';
1
+ import { Animated, Easing } from 'react-native';
2
+
3
+ const ANIMATION_SPEED = 140
2
4
 
3
5
  export const transitionSwitch = (
4
6
  active: boolean,
7
+ translate: number,
5
8
  transitionValue: Animated.Value,
6
9
  animatedColor: Animated.Value
7
10
  ): void => {
11
+
12
+ if (translate <= 0) return
13
+
8
14
  if (active) {
9
15
  Animated.timing(transitionValue, {
10
- toValue: 0,
11
- duration: 150,
12
- useNativeDriver: true,
13
- }).start();
14
- Animated.timing(animatedColor, {
15
- toValue: 0,
16
- duration: 150,
17
- useNativeDriver: false,
18
- }).start();
19
- } else {
20
- Animated.timing(transitionValue, {
21
- toValue: 16.5,
22
- duration: 150,
16
+ toValue: translate,
17
+ duration: ANIMATION_SPEED,
18
+ easing: Easing.out(Easing.ease),
23
19
  useNativeDriver: true,
24
20
  }).start();
25
21
  Animated.timing(animatedColor, {
26
22
  toValue: 1,
27
- duration: 150,
23
+ duration: ANIMATION_SPEED,
28
24
  useNativeDriver: false,
29
25
  }).start();
30
- }
26
+ return
27
+ }
28
+
29
+ Animated.timing(transitionValue, {
30
+ toValue: 0,
31
+ duration: ANIMATION_SPEED,
32
+ easing: Easing.out(Easing.ease),
33
+ useNativeDriver: true,
34
+ }).start();
35
+ Animated.timing(animatedColor, {
36
+ toValue: 0,
37
+ duration: ANIMATION_SPEED,
38
+ useNativeDriver: false,
39
+ }).start();
40
+
31
41
  };
@@ -2,21 +2,23 @@ import styled from '@emotion/native';
2
2
  import { Animated } from 'react-native';
3
3
  import { StyleProps } from '../../../types/defaults';
4
4
  import { RFValueStr } from '../../../utils';
5
- import { SwitchProps } from './Switch';
5
+
6
+ export const SWITCH_BODY_WIDTH = '40px'
7
+ export const SWITCH_PIN_WIDTH = '16px'
6
8
 
7
9
  export const StyledSwitchContent = styled(Animated.View)<
8
- Partial<SwitchProps> & Partial<StyleProps>
10
+ Partial<StyleProps>
9
11
  >`
10
12
  border-radius: ${({ theme }) => theme.borderRadius.pill};
11
13
  padding: 0 ${({ theme }) => theme.spacing.micro};
12
14
  justify-content: center;
13
- width: ${RFValueStr('40px')};
15
+ width: ${RFValueStr(SWITCH_BODY_WIDTH)};
14
16
  height: ${RFValueStr('22px')};
15
17
  `;
16
18
 
17
19
  export const StyledSwitch = styled(Animated.View)<Partial<StyleProps>>`
18
20
  border-radius: ${({ theme }) => theme.borderRadius.pill};
19
21
  background-color: ${({ theme }) => theme.miscellaneous.surfaceColor};
20
- width: ${RFValueStr('16px')};
22
+ width: ${RFValueStr(SWITCH_PIN_WIDTH)};
21
23
  height: ${RFValueStr('16px')};
22
24
  `;