1
- import { atom , reflect } from "@cn-ui/reactive"
2
- import { JSXElement } from "solid-js"
3
- import { useElementBounding } from 'solidjs-use'
4
-
1
+ import { atom , reflect } from '@cn-ui/reactive' ;
2
+ import { JSXElement } from 'solid-js' ;
3
+ import { useElementBounding } from 'solidjs-use' ;
5
4
6
5
export const AbsoluteLayout = ( props : {
7
- render : ( ) => JSXElement [ ]
8
- margin ?: number
6
+ render : ( ) => JSXElement [ ] ;
7
+ margin ?: number ;
9
8
} ) => {
10
- const collection = atom < ReturnType < typeof useElementBounding > [ ] > ( [ ] )
11
- return < div style = { {
12
- position : "relative"
13
- } } >
14
- { props . render ( ) . map ( Comp => {
15
- const ref = atom ( null )
16
- const size = useElementBounding ( ref )
17
- collection ( i => [ ...i , size ] )
18
- const originPoint = reflect ( ( ) => {
19
- const arr = collection ( )
20
- const index = arr . findIndex ( i => i === size )
21
- const stack = arr . slice ( 0 , index )
22
- return stack . reduce ( ( col , cur ) => {
23
- col . left += cur . width ( ) + ( props . margin ?? 10 )
24
- return col
25
- } , { top : 0 , left : 0 } )
26
- } )
27
- return < div ref = { ref } style = { {
28
- position : "absolute" ,
29
- display : "flex" ,
30
- top : originPoint ( ) . top + 'px' ,
31
- left : originPoint ( ) . left + 'px'
32
- } } >
33
- { Comp }
34
- </ div >
35
- } ) }
36
-
37
- </ div >
38
- }
9
+ const collection = atom < ReturnType < typeof useElementBounding > [ ] > ( [ ] ) ;
10
+ return (
11
+ < div
12
+ style = { {
13
+ position : 'relative' ,
14
+ display : 'flex' ,
15
+ } }
16
+ >
17
+ { props . render ( ) . map ( ( Comp ) => {
18
+ const ref = atom ( null ) ;
19
+ const size = useElementBounding ( ref ) ;
20
+ collection ( ( i ) => [ ...i , size ] ) ;
21
+ const originPoint = reflect ( ( ) => {
22
+ const arr = collection ( ) ;
23
+ const index = arr . findIndex ( ( i ) => i === size ) ;
24
+ const stack = arr . slice ( 0 , index ) ;
25
+ return stack . reduce (
26
+ ( col , cur ) => {
27
+ col . left += Math . ceil (
28
+ cur . width ( ) + ( props . margin ?? 10 ) ,
29
+ ) ;
30
+ return col ;
31
+ } ,
32
+ { top : 0 , left : 0 } ,
33
+ ) ;
34
+ } ) ;
35
+ return (
36
+ < div
37
+ ref = { ref }
38
+ style = { {
39
+ position : 'absolute' ,
40
+ 'max-width' : '50%' ,
41
+ top : originPoint ( ) . top + 'px' ,
42
+ left : originPoint ( ) . left + 'px' ,
43
+ } }
44
+ >
45
+ { Comp }
46
+ </ div >
47
+ ) ;
48
+ } ) }
49
+ </ div >
50
+ ) ;
51
+ } ;
0 commit comments