@fountain-ui/lab 2.0.0-beta.42 → 2.0.0-beta.44

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 (30) hide show
  1. package/build/commonjs/BottomSheet/BottomSheetNative.js +24 -3
  2. package/build/commonjs/BottomSheet/BottomSheetNative.js.map +1 -1
  3. package/build/commonjs/BottomSheet/BottomSheetProps.js.map +1 -1
  4. package/build/commonjs/BottomSheet/BottomSheetWeb.js +24 -25
  5. package/build/commonjs/BottomSheet/BottomSheetWeb.js.map +1 -1
  6. package/build/commonjs/BottomSheet/useDynamicSnapPoints.js +37 -21
  7. package/build/commonjs/BottomSheet/useDynamicSnapPoints.js.map +1 -1
  8. package/build/module/BottomSheet/BottomSheetNative.js +25 -5
  9. package/build/module/BottomSheet/BottomSheetNative.js.map +1 -1
  10. package/build/module/BottomSheet/BottomSheetProps.js.map +1 -1
  11. package/build/module/BottomSheet/BottomSheetWeb.js +24 -26
  12. package/build/module/BottomSheet/BottomSheetWeb.js.map +1 -1
  13. package/build/module/BottomSheet/useDynamicSnapPoints.js +33 -22
  14. package/build/module/BottomSheet/useDynamicSnapPoints.js.map +1 -1
  15. package/build/typescript/BottomSheet/BottomSheetProps.d.ts +13 -1
  16. package/build/typescript/BottomSheet/useDynamicSnapPoints.d.ts +7 -6
  17. package/build/typescript/Carousel/Carousel.d.ts +1 -1
  18. package/build/typescript/ViewPager/ViewPager.d.ts +1 -1
  19. package/build/typescript/ViewPager/ViewPager.native.d.ts +1 -1
  20. package/package.json +4 -4
  21. package/src/BottomSheet/BottomSheetNative.tsx +28 -3
  22. package/src/BottomSheet/BottomSheetProps.ts +15 -1
  23. package/src/BottomSheet/BottomSheetWeb.tsx +31 -25
  24. package/src/BottomSheet/useDynamicSnapPoints.ts +49 -28
  25. package/build/commonjs/BottomSheet/BottomSheet.js +0 -177
  26. package/build/commonjs/BottomSheet/BottomSheet.js.map +0 -1
  27. package/build/module/BottomSheet/BottomSheet.js +0 -161
  28. package/build/module/BottomSheet/BottomSheet.js.map +0 -1
  29. package/build/typescript/BottomSheet/BottomSheet.d.ts +0 -2
  30. package/src/BottomSheet/BottomSheet.tsx +0 -184
@@ -1,41 +1,52 @@
1
- import { useCallback, useState } from 'react';
1
+ import * as R from 'ramda';
2
+ import { useCallback, useMemo, useState } from 'react';
2
3
  import { useWindowDimensions } from 'react-native';
3
4
 
4
- const convertHeightAsPixel = (windowHeight, value) => {
5
- if (typeof value === 'number') {
6
- return value;
5
+ const convertSnapPoint = (snapPoint, windowHeight, maxDynamicContentSize) => {
6
+ if (typeof snapPoint === 'number') {
7
+ return Math.min(maxDynamicContentSize, snapPoint);
7
8
  }
8
9
 
9
10
  const percentageRegex = new RegExp(/^[0-9]+%$/);
10
11
 
11
- if (percentageRegex.test(value)) {
12
- const percentage = parseFloat(value) / 100;
13
- return isNaN(percentage) ? 0 : Math.round(windowHeight * percentage);
12
+ if (percentageRegex.test(snapPoint)) {
13
+ const percentage = parseFloat(snapPoint) / 100;
14
+ return isNaN(percentage) ? 0 : Math.min(maxDynamicContentSize, Math.round(windowHeight * percentage));
14
15
  }
15
16
 
16
17
  return 0;
17
18
  };
18
19
 
19
- export default function useDynamicSnapPoints(props) {
20
+ export default function useDynamicSnapPoints(params) {
20
21
  const {
21
- index: currentIndex,
22
- initialSnapPoints
23
- } = props;
22
+ enableDynamicSizing,
23
+ maxHeightNormalizedRatio,
24
+ snapPoints
25
+ } = params;
24
26
  const {
25
- height: WINDOW_HEIGHT
27
+ height: windowHeight
26
28
  } = useWindowDimensions();
27
- const [snapPoints, setSnapPoints] = useState(initialSnapPoints.map(snapPoint => convertHeightAsPixel(WINDOW_HEIGHT, snapPoint)));
28
- const handleContentLayout = useCallback(event => {
29
- if (initialSnapPoints[currentIndex] === 'CONTENT_HEIGHT') {
30
- const contentHeight = Math.min(Math.round(WINDOW_HEIGHT * 0.9), event.nativeEvent.layout.height);
31
- setSnapPoints(prevState => prevState.map((snapPoint, index) => {
32
- return currentIndex === index ? contentHeight : snapPoint;
33
- }));
29
+ const [contentHeight, setContentHeight] = useState(0);
30
+ const convertedSnapPoints = useMemo(() => {
31
+ const maxDynamicContentSize = Math.round(windowHeight * maxHeightNormalizedRatio);
32
+ const convertedSnapPoints = snapPoints.map(snapPoint => {
33
+ return convertSnapPoint(snapPoint, windowHeight, maxDynamicContentSize);
34
+ });
35
+
36
+ if (enableDynamicSizing && contentHeight !== 0) {
37
+ convertedSnapPoints.push(convertSnapPoint(contentHeight, windowHeight, maxDynamicContentSize));
34
38
  }
35
- }, [currentIndex, initialSnapPoints]);
39
+
40
+ return Array.from(new Set(convertedSnapPoints)).sort((a, b) => a - b);
41
+ }, [contentHeight, snapPoints, windowHeight]);
42
+ const highestSnapPoint = R.last(convertedSnapPoints) ?? 0;
43
+ const handleContentSizeChange = useCallback((_, height) => {
44
+ setContentHeight(height);
45
+ }, []);
36
46
  return {
37
- handleContentLayout,
38
- snapPoints
47
+ convertedSnapPoints,
48
+ handleContentSizeChange,
49
+ highestSnapPoint
39
50
  };
40
51
  }
41
52
  //# sourceMappingURL=useDynamicSnapPoints.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["useCallback","useState","useWindowDimensions","convertHeightAsPixel","windowHeight","value","percentageRegex","RegExp","test","percentage","parseFloat","isNaN","Math","round","useDynamicSnapPoints","props","index","currentIndex","initialSnapPoints","height","WINDOW_HEIGHT","snapPoints","setSnapPoints","map","snapPoint","handleContentLayout","event","contentHeight","min","nativeEvent","layout","prevState"],"sources":["useDynamicSnapPoints.ts"],"sourcesContent":["import { useCallback, useState } from 'react';\nimport { LayoutChangeEvent, useWindowDimensions } from 'react-native';\n\ninterface UseDynamicSnapPointsParams {\n index: number;\n initialSnapPoints: (number | string)[];\n}\n\ninterface UseDynamicSnapPointsReturns {\n handleContentLayout: (e: LayoutChangeEvent) => void;\n snapPoints: number[];\n}\n\nconst convertHeightAsPixel = (windowHeight: number, value: number | string): number => {\n if (typeof value === 'number') {\n return value;\n }\n\n const percentageRegex = new RegExp(/^[0-9]+%$/);\n if (percentageRegex.test(value)) {\n const percentage = parseFloat(value) / 100;\n return isNaN(percentage) ? 0 : Math.round(windowHeight * percentage);\n }\n\n return 0;\n};\n\nexport default function useDynamicSnapPoints(props: UseDynamicSnapPointsParams): UseDynamicSnapPointsReturns {\n const {\n index: currentIndex,\n initialSnapPoints,\n } = props;\n\n const { height: WINDOW_HEIGHT } = useWindowDimensions();\n\n const [snapPoints, setSnapPoints] = useState(initialSnapPoints.map((snapPoint) => convertHeightAsPixel(WINDOW_HEIGHT, snapPoint)));\n\n const handleContentLayout = useCallback((event: LayoutChangeEvent) => {\n if (initialSnapPoints[currentIndex] === 'CONTENT_HEIGHT') {\n const contentHeight = Math.min(Math.round(WINDOW_HEIGHT * 0.9), event.nativeEvent.layout.height);\n \n setSnapPoints(prevState => prevState.map((snapPoint, index) => {\n return currentIndex === index ? contentHeight : snapPoint;\n }));\n }\n }, [currentIndex, initialSnapPoints]);\n\n return {\n handleContentLayout,\n snapPoints,\n };\n}\n"],"mappings":"AAAA,SAASA,WAAT,EAAsBC,QAAtB,QAAsC,OAAtC;AACA,SAA4BC,mBAA5B,QAAuD,cAAvD;;AAYA,MAAMC,oBAAoB,GAAG,CAACC,YAAD,EAAuBC,KAAvB,KAA0D;EACnF,IAAI,OAAOA,KAAP,KAAiB,QAArB,EAA+B;IAC3B,OAAOA,KAAP;EACH;;EAED,MAAMC,eAAe,GAAG,IAAIC,MAAJ,CAAW,WAAX,CAAxB;;EACA,IAAID,eAAe,CAACE,IAAhB,CAAqBH,KAArB,CAAJ,EAAiC;IAC7B,MAAMI,UAAU,GAAGC,UAAU,CAACL,KAAD,CAAV,GAAoB,GAAvC;IACA,OAAOM,KAAK,CAACF,UAAD,CAAL,GAAoB,CAApB,GAAwBG,IAAI,CAACC,KAAL,CAAWT,YAAY,GAAGK,UAA1B,CAA/B;EACH;;EAED,OAAO,CAAP;AACH,CAZD;;AAcA,eAAe,SAASK,oBAAT,CAA8BC,KAA9B,EAA8F;EACzG,MAAM;IACFC,KAAK,EAAEC,YADL;IAEFC;EAFE,IAGFH,KAHJ;EAKA,MAAM;IAAEI,MAAM,EAAEC;EAAV,IAA4BlB,mBAAmB,EAArD;EAEA,MAAM,CAACmB,UAAD,EAAaC,aAAb,IAA8BrB,QAAQ,CAACiB,iBAAiB,CAACK,GAAlB,CAAuBC,SAAD,IAAerB,oBAAoB,CAACiB,aAAD,EAAgBI,SAAhB,CAAzD,CAAD,CAA5C;EAEA,MAAMC,mBAAmB,GAAGzB,WAAW,CAAE0B,KAAD,IAA8B;IAClE,IAAIR,iBAAiB,CAACD,YAAD,CAAjB,KAAoC,gBAAxC,EAA0D;MACtD,MAAMU,aAAa,GAAGf,IAAI,CAACgB,GAAL,CAAShB,IAAI,CAACC,KAAL,CAAWO,aAAa,GAAG,GAA3B,CAAT,EAA0CM,KAAK,CAACG,WAAN,CAAkBC,MAAlB,CAAyBX,MAAnE,CAAtB;MAEAG,aAAa,CAACS,SAAS,IAAIA,SAAS,CAACR,GAAV,CAAc,CAACC,SAAD,EAAYR,KAAZ,KAAsB;QAC3D,OAAOC,YAAY,KAAKD,KAAjB,GAAyBW,aAAzB,GAAyCH,SAAhD;MACH,CAF0B,CAAd,CAAb;IAGH;EACJ,CARsC,EAQpC,CAACP,YAAD,EAAeC,iBAAf,CARoC,CAAvC;EAUA,OAAO;IACHO,mBADG;IAEHJ;EAFG,CAAP;AAIH"}
1
+ {"version":3,"names":["R","useCallback","useMemo","useState","useWindowDimensions","convertSnapPoint","snapPoint","windowHeight","maxDynamicContentSize","Math","min","percentageRegex","RegExp","test","percentage","parseFloat","isNaN","round","useDynamicSnapPoints","params","enableDynamicSizing","maxHeightNormalizedRatio","snapPoints","height","contentHeight","setContentHeight","convertedSnapPoints","map","push","Array","from","Set","sort","a","b","highestSnapPoint","last","handleContentSizeChange","_"],"sources":["useDynamicSnapPoints.ts"],"sourcesContent":["import * as R from 'ramda';\nimport { useCallback, useMemo, useState } from 'react';\nimport { useWindowDimensions } from 'react-native';\n\ninterface UseDynamicSnapPointsParams {\n enableDynamicSizing: boolean;\n maxHeightNormalizedRatio: number;\n snapPoints: (number | string)[];\n}\n\ninterface UseDynamicSnapPointsReturns {\n convertedSnapPoints: number[];\n handleContentSizeChange: (w: number, h: number) => void;\n highestSnapPoint: number;\n}\n\nconst convertSnapPoint = (snapPoint: number | string, windowHeight: number, maxDynamicContentSize: number): number => {\n if (typeof snapPoint === 'number') {\n return Math.min(maxDynamicContentSize, snapPoint);\n }\n\n const percentageRegex = new RegExp(/^[0-9]+%$/);\n if (percentageRegex.test(snapPoint)) {\n const percentage = parseFloat(snapPoint) / 100;\n return isNaN(percentage) ? 0 : Math.min(maxDynamicContentSize, Math.round(windowHeight * percentage));\n }\n\n return 0;\n};\n\nexport default function useDynamicSnapPoints(params: UseDynamicSnapPointsParams): UseDynamicSnapPointsReturns {\n const {\n enableDynamicSizing,\n maxHeightNormalizedRatio,\n snapPoints,\n } = params;\n\n const { height: windowHeight } = useWindowDimensions();\n\n const [contentHeight, setContentHeight] = useState<number>(0);\n\n const convertedSnapPoints = useMemo(() => {\n const maxDynamicContentSize = Math.round(windowHeight * maxHeightNormalizedRatio);\n\n const convertedSnapPoints = snapPoints.map((snapPoint) => {\n return convertSnapPoint(snapPoint, windowHeight, maxDynamicContentSize);\n });\n\n if (enableDynamicSizing && contentHeight !== 0) {\n convertedSnapPoints.push(\n convertSnapPoint(contentHeight, windowHeight, maxDynamicContentSize),\n );\n }\n\n return Array.from(new Set(convertedSnapPoints)).sort((a, b) => a - b);\n }, [\n contentHeight,\n snapPoints,\n windowHeight,\n ]);\n\n const highestSnapPoint = R.last(convertedSnapPoints) ?? 0;\n\n const handleContentSizeChange = useCallback((_: number, height: number) => {\n setContentHeight(height);\n }, []);\n\n return {\n convertedSnapPoints,\n handleContentSizeChange,\n highestSnapPoint,\n };\n}\n"],"mappings":"AAAA,OAAO,KAAKA,CAAZ,MAAmB,OAAnB;AACA,SAASC,WAAT,EAAsBC,OAAtB,EAA+BC,QAA/B,QAA+C,OAA/C;AACA,SAASC,mBAAT,QAAoC,cAApC;;AAcA,MAAMC,gBAAgB,GAAG,CAACC,SAAD,EAA6BC,YAA7B,EAAmDC,qBAAnD,KAA6F;EAClH,IAAI,OAAOF,SAAP,KAAqB,QAAzB,EAAmC;IAC/B,OAAOG,IAAI,CAACC,GAAL,CAASF,qBAAT,EAAgCF,SAAhC,CAAP;EACH;;EAED,MAAMK,eAAe,GAAG,IAAIC,MAAJ,CAAW,WAAX,CAAxB;;EACA,IAAID,eAAe,CAACE,IAAhB,CAAqBP,SAArB,CAAJ,EAAqC;IACjC,MAAMQ,UAAU,GAAGC,UAAU,CAACT,SAAD,CAAV,GAAwB,GAA3C;IACA,OAAOU,KAAK,CAACF,UAAD,CAAL,GAAoB,CAApB,GAAwBL,IAAI,CAACC,GAAL,CAASF,qBAAT,EAAgCC,IAAI,CAACQ,KAAL,CAAWV,YAAY,GAAGO,UAA1B,CAAhC,CAA/B;EACH;;EAED,OAAO,CAAP;AACH,CAZD;;AAcA,eAAe,SAASI,oBAAT,CAA8BC,MAA9B,EAA+F;EAC1G,MAAM;IACFC,mBADE;IAEFC,wBAFE;IAGFC;EAHE,IAIFH,MAJJ;EAMA,MAAM;IAAEI,MAAM,EAAEhB;EAAV,IAA2BH,mBAAmB,EAApD;EAEA,MAAM,CAACoB,aAAD,EAAgBC,gBAAhB,IAAoCtB,QAAQ,CAAS,CAAT,CAAlD;EAEA,MAAMuB,mBAAmB,GAAGxB,OAAO,CAAC,MAAM;IACtC,MAAMM,qBAAqB,GAAGC,IAAI,CAACQ,KAAL,CAAWV,YAAY,GAAGc,wBAA1B,CAA9B;IAEA,MAAMK,mBAAmB,GAAGJ,UAAU,CAACK,GAAX,CAAgBrB,SAAD,IAAe;MACtD,OAAOD,gBAAgB,CAACC,SAAD,EAAYC,YAAZ,EAA0BC,qBAA1B,CAAvB;IACH,CAF2B,CAA5B;;IAIA,IAAIY,mBAAmB,IAAII,aAAa,KAAK,CAA7C,EAAgD;MAC5CE,mBAAmB,CAACE,IAApB,CACIvB,gBAAgB,CAACmB,aAAD,EAAgBjB,YAAhB,EAA8BC,qBAA9B,CADpB;IAGH;;IAED,OAAOqB,KAAK,CAACC,IAAN,CAAW,IAAIC,GAAJ,CAAQL,mBAAR,CAAX,EAAyCM,IAAzC,CAA8C,CAACC,CAAD,EAAIC,CAAJ,KAAUD,CAAC,GAAGC,CAA5D,CAAP;EACH,CAdkC,EAchC,CACCV,aADD,EAECF,UAFD,EAGCf,YAHD,CAdgC,CAAnC;EAoBA,MAAM4B,gBAAgB,GAAGnC,CAAC,CAACoC,IAAF,CAAOV,mBAAP,KAA+B,CAAxD;EAEA,MAAMW,uBAAuB,GAAGpC,WAAW,CAAC,CAACqC,CAAD,EAAYf,MAAZ,KAA+B;IACvEE,gBAAgB,CAACF,MAAD,CAAhB;EACH,CAF0C,EAExC,EAFwC,CAA3C;EAIA,OAAO;IACHG,mBADG;IAEHW,uBAFG;IAGHF;EAHG,CAAP;AAKH"}
@@ -10,6 +10,11 @@ export default interface BottomSheetProps extends ComponentProps<{
10
10
  * BottomSheet children, usually the included sub-components.
11
11
  */
12
12
  children?: React.ReactNode;
13
+ /**
14
+ * Enable dynamic sizing for content size.
15
+ * @default true
16
+ */
17
+ enableDynamicSizing?: boolean;
13
18
  /**
14
19
  * Area to be fixed on the top of the bottom sheet.
15
20
  */
@@ -18,6 +23,12 @@ export default interface BottomSheetProps extends ComponentProps<{
18
23
  * Snap index. You could also provide -1 to bottom sheet in closed state.
19
24
  */
20
25
  index: number;
26
+ /**
27
+ * Maximum height(normalized value) of dialog
28
+ * ex. 30% => 0.3 / 90% => 0.9
29
+ * @default 0.9
30
+ */
31
+ maxHeightNormalizedRatio?: number;
21
32
  /**
22
33
  * Callback fired when the index is changed.
23
34
  * Important! Use memoized value.
@@ -27,7 +38,8 @@ export default interface BottomSheetProps extends ComponentProps<{
27
38
  * Points for the bottom sheet to snap to, points should be sorted from bottom to top.
28
39
  * Important! Use memoized value.
29
40
  * Only number type or string type(~% format) can be used.
41
+ * @default []
30
42
  */
31
- snapPoints: Array<number | string>;
43
+ snapPoints?: Array<number | string>;
32
44
  }> {
33
45
  }
@@ -1,11 +1,12 @@
1
- import { LayoutChangeEvent } from 'react-native';
2
1
  interface UseDynamicSnapPointsParams {
3
- index: number;
4
- initialSnapPoints: (number | string)[];
2
+ enableDynamicSizing: boolean;
3
+ maxHeightNormalizedRatio: number;
4
+ snapPoints: (number | string)[];
5
5
  }
6
6
  interface UseDynamicSnapPointsReturns {
7
- handleContentLayout: (e: LayoutChangeEvent) => void;
8
- snapPoints: number[];
7
+ convertedSnapPoints: number[];
8
+ handleContentSizeChange: (w: number, h: number) => void;
9
+ highestSnapPoint: number;
9
10
  }
10
- export default function useDynamicSnapPoints(props: UseDynamicSnapPointsParams): UseDynamicSnapPointsReturns;
11
+ export default function useDynamicSnapPoints(params: UseDynamicSnapPointsParams): UseDynamicSnapPointsReturns;
11
12
  export {};
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
2
  import type CarouselProps from './CarouselProps';
3
3
  import type { CarouselInstance } from './types';
4
- declare const _default: React.MemoExoticComponent<React.ForwardRefExoticComponent<Pick<CarouselProps<any>, "style" | "data" | "autoplay" | "autoplayInterval" | "createItemStyle" | "createScrollAnimation" | "disableSmartAutoplay" | "initialIndex" | "itemHeight" | "itemWidth" | "loop" | "onIndexChange" | "renderItem" | "scrollEnabled" | "windowSize"> & React.RefAttributes<CarouselInstance>>>;
4
+ declare const _default: React.MemoExoticComponent<React.ForwardRefExoticComponent<Pick<CarouselProps<any>, "style" | "data" | "scrollEnabled" | "autoplay" | "autoplayInterval" | "createItemStyle" | "createScrollAnimation" | "disableSmartAutoplay" | "initialIndex" | "itemHeight" | "itemWidth" | "loop" | "onIndexChange" | "renderItem" | "windowSize"> & React.RefAttributes<CarouselInstance>>>;
5
5
  export default _default;
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
2
  import type ViewPagerProps from './ViewPagerProps';
3
3
  import type { ViewPagerInstance } from './types';
4
- declare const ViewPager: React.ForwardRefExoticComponent<Pick<ViewPagerProps, "style" | "children" | "onChange" | "scrollEnabled" | "keyboardDismissMode" | "initialPage" | "loading" | "offscreenPageRerenderLimit" | "pageForceRerenderKey" | "UNSTABLE_sharedPage"> & React.RefAttributes<ViewPagerInstance>>;
4
+ declare const ViewPager: React.ForwardRefExoticComponent<Pick<ViewPagerProps, "style" | "children" | "onChange" | "keyboardDismissMode" | "scrollEnabled" | "initialPage" | "loading" | "offscreenPageRerenderLimit" | "pageForceRerenderKey" | "UNSTABLE_sharedPage"> & React.RefAttributes<ViewPagerInstance>>;
5
5
  export default ViewPager;
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
2
  import type ViewPagerProps from './ViewPagerProps';
3
3
  import type { ViewPagerInstance } from './types';
4
- declare const ViewPager: React.ForwardRefExoticComponent<Pick<ViewPagerProps, "style" | "children" | "onChange" | "scrollEnabled" | "keyboardDismissMode" | "initialPage" | "loading" | "offscreenPageRerenderLimit" | "pageForceRerenderKey" | "UNSTABLE_sharedPage"> & React.RefAttributes<ViewPagerInstance>>;
4
+ declare const ViewPager: React.ForwardRefExoticComponent<Pick<ViewPagerProps, "style" | "children" | "onChange" | "keyboardDismissMode" | "scrollEnabled" | "initialPage" | "loading" | "offscreenPageRerenderLimit" | "pageForceRerenderKey" | "UNSTABLE_sharedPage"> & React.RefAttributes<ViewPagerInstance>>;
5
5
  export default ViewPager;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fountain-ui/lab",
3
- "version": "2.0.0-beta.42",
3
+ "version": "2.0.0-beta.44",
4
4
  "private": false,
5
5
  "author": "Fountain-UI Team",
6
6
  "description": "Incubator for Fountain-UI React components.",
@@ -23,7 +23,7 @@
23
23
  },
24
24
  "peerDependencies": {
25
25
  "@fountain-ui/core": "^2.0.0-beta.3",
26
- "@gorhom/bottom-sheet": "^4.1.3",
26
+ "@gorhom/bottom-sheet": "^4.5.0",
27
27
  "date-fns": "^2.0.0",
28
28
  "react": "^16.8.0 || ^17.0.0",
29
29
  "react-dom": "^16.8.0 || ^17.0.0",
@@ -43,7 +43,7 @@
43
43
  }
44
44
  },
45
45
  "devDependencies": {
46
- "@gorhom/bottom-sheet": "4.1.3",
46
+ "@gorhom/bottom-sheet": "^4.5.0",
47
47
  "date-fns": "^2.23.0",
48
48
  "react-native-pager-view": "^4.2.4",
49
49
  "react-native-safe-area-context": "^4.0.0"
@@ -70,5 +70,5 @@
70
70
  "publishConfig": {
71
71
  "access": "public"
72
72
  },
73
- "gitHead": "263ed4f4ec14ca755180bad17b877d9ab9658e52"
73
+ "gitHead": "d6c9346f8fd3675ac5096ee5caaad3f372f9def6"
74
74
  }
@@ -1,11 +1,12 @@
1
1
  import React from 'react';
2
- import { Platform } from 'react-native';
2
+ import { Platform, useWindowDimensions, View } from 'react-native';
3
3
  import { useTheme } from '@fountain-ui/styles';
4
4
  import {
5
5
  BottomSheetBackdrop,
6
6
  BottomSheetBackdropProps,
7
7
  BottomSheetModal,
8
8
  BottomSheetModalProvider,
9
+ BottomSheetScrollView,
9
10
  } from '@gorhom/bottom-sheet';
10
11
  import type BottomSheetProps from './BottomSheetProps';
11
12
  import TransparentBackdrop from './TransparentBackdrop';
@@ -16,14 +17,20 @@ export default function BottomSheet(props: BottomSheetProps) {
16
17
  const {
17
18
  backdropOpacity = 0.5,
18
19
  children,
20
+ enableDynamicSizing = true,
21
+ header,
19
22
  index,
23
+ maxHeightNormalizedRatio = 0.9,
20
24
  onChange,
21
- snapPoints,
25
+ snapPoints = [],
22
26
  } = props;
23
27
 
24
28
  const indexRef = React.useRef<number>(-1);
25
29
  const bottomSheetRef = React.useRef<BottomSheetModal | null>(null);
26
30
 
31
+ const { height: windowHeight } = useWindowDimensions();
32
+ const maxDynamicContentSize = Math.round(windowHeight * maxHeightNormalizedRatio);
33
+
27
34
  const handleChange = React.useCallback((newIndex: number) => {
28
35
  indexRef.current = newIndex;
29
36
 
@@ -64,6 +71,12 @@ export default function BottomSheet(props: BottomSheetProps) {
64
71
  const backgroundStyle = {
65
72
  backgroundColor: theme.palette.paper.default,
66
73
  };
74
+ const contentWrapperStyle = {
75
+ flex: 1,
76
+ borderTopLeftRadius: 15,
77
+ borderTopRightRadius: 15,
78
+ overflow: 'hidden',
79
+ };
67
80
 
68
81
  const isBackdropTransparent = backdropOpacity <= 0;
69
82
 
@@ -91,8 +104,20 @@ export default function BottomSheet(props: BottomSheetProps) {
91
104
  snapPoints={snapPoints}
92
105
  style={modalStyle}
93
106
  enablePanDownToClose={Boolean(onChange)}
107
+ enableDynamicSizing={enableDynamicSizing}
108
+ maxDynamicContentSize={maxDynamicContentSize}
94
109
  >
95
- {children}
110
+ {/* @ts-ignore */}
111
+ <View style={contentWrapperStyle}>
112
+ <BottomSheetScrollView
113
+ bounces={false}
114
+ stickyHeaderIndices={header ? [0] : undefined}
115
+ >
116
+ {header}
117
+
118
+ {children}
119
+ </BottomSheetScrollView>
120
+ </View>
96
121
  </BottomSheetModal>
97
122
  </BottomSheetModalProvider>
98
123
  );
@@ -13,6 +13,12 @@ export default interface BottomSheetProps extends ComponentProps<{
13
13
  */
14
14
  children?: React.ReactNode;
15
15
 
16
+ /**
17
+ * Enable dynamic sizing for content size.
18
+ * @default true
19
+ */
20
+ enableDynamicSizing?: boolean;
21
+
16
22
  /**
17
23
  * Area to be fixed on the top of the bottom sheet.
18
24
  */
@@ -23,6 +29,13 @@ export default interface BottomSheetProps extends ComponentProps<{
23
29
  */
24
30
  index: number;
25
31
 
32
+ /**
33
+ * Maximum height(normalized value) of dialog
34
+ * ex. 30% => 0.3 / 90% => 0.9
35
+ * @default 0.9
36
+ */
37
+ maxHeightNormalizedRatio?: number;
38
+
26
39
  /**
27
40
  * Callback fired when the index is changed.
28
41
  * Important! Use memoized value.
@@ -33,6 +46,7 @@ export default interface BottomSheetProps extends ComponentProps<{
33
46
  * Points for the bottom sheet to snap to, points should be sorted from bottom to top.
34
47
  * Important! Use memoized value.
35
48
  * Only number type or string type(~% format) can be used.
49
+ * @default []
36
50
  */
37
- snapPoints: Array<number | string>;
51
+ snapPoints?: Array<number | string>;
38
52
  }> {}
@@ -1,9 +1,10 @@
1
1
  import React from 'react';
2
- import { useWindowDimensions } from 'react-native';
2
+ import { ScrollView } from 'react-native';
3
3
  import { Modal, Paper, StyleSheet, css, useTheme } from '@fountain-ui/core';
4
4
  import { NamedStylesStringUnion, UseStyles } from '@fountain-ui/styles';
5
5
  import AnimatedY from '../AnimatedY';
6
6
  import type BottomSheetProps from './BottomSheetProps';
7
+ import useDynamicSnapPoints from './useDynamicSnapPoints';
7
8
 
8
9
  type BottomSheetStyles = NamedStylesStringUnion<'root' | 'animated' | 'paper'>;
9
10
 
@@ -29,41 +30,42 @@ const useStyles: UseStyles<BottomSheetStyles> = function (): BottomSheetStyles {
29
30
  };
30
31
  };
31
32
 
32
- const convertHeightAsPixel = (windowHeight: number, value: number | string): number => {
33
- if (typeof value === 'number') {
34
- return value;
35
- }
36
-
37
- const percentageRegex = new RegExp(/^[0-9]+%$/);
38
- if (percentageRegex.test(value)) {
39
- const percentage = parseFloat(value) / 100;
40
- return isNaN(percentage) ? 0 : Math.round(windowHeight * percentage);
41
- }
42
-
43
- return 0;
44
- };
45
-
46
33
  export default function BottomSheet(props: BottomSheetProps) {
47
34
  const {
48
35
  backdropOpacity,
49
36
  children,
37
+ enableDynamicSizing = true,
38
+ header,
50
39
  index,
40
+ maxHeightNormalizedRatio = 0.9,
51
41
  onChange,
52
- snapPoints,
42
+ snapPoints = [],
53
43
  } = props;
54
44
 
55
45
  const styles = useStyles();
56
46
 
57
- const { height: windowHeight } = useWindowDimensions();
58
-
59
47
  const handleClose = () => {
60
48
  if (onChange) {
61
49
  onChange(-1);
62
50
  }
63
51
  };
64
52
 
65
- const height = convertHeightAsPixel(windowHeight, snapPoints[snapPoints.length - 1] ?? 0);
66
- const translateY = height - (convertHeightAsPixel(windowHeight, snapPoints[index] ?? 0));
53
+ const {
54
+ convertedSnapPoints,
55
+ handleContentSizeChange,
56
+ highestSnapPoint,
57
+ } = useDynamicSnapPoints({
58
+ enableDynamicSizing,
59
+ maxHeightNormalizedRatio,
60
+ snapPoints,
61
+ });
62
+
63
+ const translateY = highestSnapPoint - (convertedSnapPoints[index] ?? 0);
64
+
65
+ const paperStyles = [
66
+ styles.paper,
67
+ { height: highestSnapPoint },
68
+ ];
67
69
 
68
70
  return (
69
71
  <Modal
@@ -78,12 +80,16 @@ export default function BottomSheet(props: BottomSheetProps) {
78
80
  >
79
81
  <Paper
80
82
  elevation={12}
81
- style={[
82
- styles.paper,
83
- { height },
84
- ]}
83
+ style={paperStyles}
85
84
  >
86
- {children}
85
+ <ScrollView
86
+ onContentSizeChange={handleContentSizeChange}
87
+ stickyHeaderIndices={header ? [0] : undefined}
88
+ >
89
+ {header}
90
+
91
+ {children}
92
+ </ScrollView>
87
93
  </Paper>
88
94
  </AnimatedY>
89
95
  </Modal>
@@ -1,52 +1,73 @@
1
- import { useCallback, useState } from 'react';
2
- import { LayoutChangeEvent, useWindowDimensions } from 'react-native';
1
+ import * as R from 'ramda';
2
+ import { useCallback, useMemo, useState } from 'react';
3
+ import { useWindowDimensions } from 'react-native';
3
4
 
4
5
  interface UseDynamicSnapPointsParams {
5
- index: number;
6
- initialSnapPoints: (number | string)[];
6
+ enableDynamicSizing: boolean;
7
+ maxHeightNormalizedRatio: number;
8
+ snapPoints: (number | string)[];
7
9
  }
8
10
 
9
11
  interface UseDynamicSnapPointsReturns {
10
- handleContentLayout: (e: LayoutChangeEvent) => void;
11
- snapPoints: number[];
12
+ convertedSnapPoints: number[];
13
+ handleContentSizeChange: (w: number, h: number) => void;
14
+ highestSnapPoint: number;
12
15
  }
13
16
 
14
- const convertHeightAsPixel = (windowHeight: number, value: number | string): number => {
15
- if (typeof value === 'number') {
16
- return value;
17
+ const convertSnapPoint = (snapPoint: number | string, windowHeight: number, maxDynamicContentSize: number): number => {
18
+ if (typeof snapPoint === 'number') {
19
+ return Math.min(maxDynamicContentSize, snapPoint);
17
20
  }
18
21
 
19
22
  const percentageRegex = new RegExp(/^[0-9]+%$/);
20
- if (percentageRegex.test(value)) {
21
- const percentage = parseFloat(value) / 100;
22
- return isNaN(percentage) ? 0 : Math.round(windowHeight * percentage);
23
+ if (percentageRegex.test(snapPoint)) {
24
+ const percentage = parseFloat(snapPoint) / 100;
25
+ return isNaN(percentage) ? 0 : Math.min(maxDynamicContentSize, Math.round(windowHeight * percentage));
23
26
  }
24
27
 
25
28
  return 0;
26
29
  };
27
30
 
28
- export default function useDynamicSnapPoints(props: UseDynamicSnapPointsParams): UseDynamicSnapPointsReturns {
31
+ export default function useDynamicSnapPoints(params: UseDynamicSnapPointsParams): UseDynamicSnapPointsReturns {
29
32
  const {
30
- index: currentIndex,
31
- initialSnapPoints,
32
- } = props;
33
+ enableDynamicSizing,
34
+ maxHeightNormalizedRatio,
35
+ snapPoints,
36
+ } = params;
37
+
38
+ const { height: windowHeight } = useWindowDimensions();
33
39
 
34
- const { height: WINDOW_HEIGHT } = useWindowDimensions();
40
+ const [contentHeight, setContentHeight] = useState<number>(0);
35
41
 
36
- const [snapPoints, setSnapPoints] = useState(initialSnapPoints.map((snapPoint) => convertHeightAsPixel(WINDOW_HEIGHT, snapPoint)));
42
+ const convertedSnapPoints = useMemo(() => {
43
+ const maxDynamicContentSize = Math.round(windowHeight * maxHeightNormalizedRatio);
37
44
 
38
- const handleContentLayout = useCallback((event: LayoutChangeEvent) => {
39
- if (initialSnapPoints[currentIndex] === 'CONTENT_HEIGHT') {
40
- const contentHeight = Math.min(Math.round(WINDOW_HEIGHT * 0.9), event.nativeEvent.layout.height);
41
-
42
- setSnapPoints(prevState => prevState.map((snapPoint, index) => {
43
- return currentIndex === index ? contentHeight : snapPoint;
44
- }));
45
+ const convertedSnapPoints = snapPoints.map((snapPoint) => {
46
+ return convertSnapPoint(snapPoint, windowHeight, maxDynamicContentSize);
47
+ });
48
+
49
+ if (enableDynamicSizing && contentHeight !== 0) {
50
+ convertedSnapPoints.push(
51
+ convertSnapPoint(contentHeight, windowHeight, maxDynamicContentSize),
52
+ );
45
53
  }
46
- }, [currentIndex, initialSnapPoints]);
47
54
 
48
- return {
49
- handleContentLayout,
55
+ return Array.from(new Set(convertedSnapPoints)).sort((a, b) => a - b);
56
+ }, [
57
+ contentHeight,
50
58
  snapPoints,
59
+ windowHeight,
60
+ ]);
61
+
62
+ const highestSnapPoint = R.last(convertedSnapPoints) ?? 0;
63
+
64
+ const handleContentSizeChange = useCallback((_: number, height: number) => {
65
+ setContentHeight(height);
66
+ }, []);
67
+
68
+ return {
69
+ convertedSnapPoints,
70
+ handleContentSizeChange,
71
+ highestSnapPoint,
51
72
  };
52
73
  }