@@ -4,32 +4,31 @@ import styled, { css } from 'styled-components'
4
4
interface Props {
5
5
height : string
6
6
width : string
7
- color : string
8
7
}
9
8
10
9
interface HorizontalShadowProps {
11
10
showOnLeft : boolean
12
11
showOnRight : boolean
13
- offsetRight ?: number
12
+ rightOffset ?: number
14
13
}
15
14
16
15
interface VerticalShadowProps {
17
16
showOnTop : boolean
18
17
showOnBottom : boolean
19
- offsetDown ?: number
18
+ bottomOffset ?: number
20
19
}
21
20
22
21
const ShadowHorizontal = styled . div < HorizontalShadowProps > `
23
22
position: relative;
24
- ${ props =>
25
- props . showOnRight
23
+ ${ ( { showOnRight , rightOffset = 0 } ) =>
24
+ showOnRight
26
25
? css `
27
26
::after {
28
27
content : '' ;
29
28
position : absolute;
30
29
top : 0 ;
31
30
bottom : 0 ;
32
- right : ${ props . offsetRight || 0 } px;
31
+ right : ${ rightOffset } px;
33
32
width : 30px ;
34
33
background : linear-gradient (
35
34
to right,
@@ -39,8 +38,8 @@ const ShadowHorizontal = styled.div<HorizontalShadowProps>`
39
38
}
40
39
`
41
40
: '' }
42
- ${ props =>
43
- props . showOnLeft
41
+ ${ ( { showOnLeft } ) =>
42
+ showOnLeft
44
43
? css `
45
44
::before {
46
45
content : '' ;
@@ -61,8 +60,8 @@ const ShadowHorizontal = styled.div<HorizontalShadowProps>`
61
60
62
61
const ShadowVertical = styled . div < VerticalShadowProps > `
63
62
position: relative;
64
- ${ props =>
65
- props . showOnTop
63
+ ${ ( { showOnTop } ) =>
64
+ showOnTop
66
65
? css `
67
66
::before {
68
67
content : '' ;
@@ -79,13 +78,13 @@ const ShadowVertical = styled.div<VerticalShadowProps>`
79
78
}
80
79
`
81
80
: '' }
82
- ${ props =>
83
- props . showOnBottom
81
+ ${ ( { showOnBottom , bottomOffset = 0 } ) =>
82
+ showOnBottom
84
83
? css `
85
84
::after {
86
85
content : '' ;
87
86
position : absolute;
88
- bottom : ${ props . offsetDown || 0 } px;
87
+ bottom : ${ bottomOffset } px;
89
88
left : 0 ;
90
89
right : 0 ;
91
90
height : 30px ;
@@ -106,73 +105,6 @@ const Container = styled.div<Props>`
106
105
display: flex;
107
106
`
108
107
109
- export const VerticalScrollShadow : FC < Props > = ( { children, ...props } ) => {
110
- const ref = useRef < HTMLDivElement > ( null )
111
-
112
- const [ showOnBottom , setShowOnBottom ] = useState < boolean > ( true )
113
- const [ showOnTop , setShowOnTop ] = useState < boolean > ( false )
114
-
115
- useEffect ( ( ) => {
116
- const onScroll = ( ) => {
117
- const {
118
- scrollHeight = 0 ,
119
- scrollTop = 0 ,
120
- offsetHeight = 0 ,
121
- } = ref . current || { }
122
- setShowOnTop ( scrollTop > 0 )
123
- setShowOnBottom ( scrollTop + offsetHeight < scrollHeight )
124
- }
125
- onScroll ( )
126
- console . log ( ref )
127
- const node = ref . current
128
- node ?. addEventListener ( 'scroll' , onScroll )
129
- return ( ) => {
130
- node ?. removeEventListener ( 'scroll' , onScroll )
131
- }
132
- } , [ ] )
133
-
134
- return (
135
- < ShadowVertical showOnTop = { showOnTop } showOnBottom = { showOnBottom } >
136
- < Container ref = { ref } { ...props } >
137
- { children }
138
- </ Container >
139
- </ ShadowVertical >
140
- )
141
- }
142
-
143
- export const HorizontalScrollShadow : FC < Props > = ( { children, ...props } ) => {
144
- const ref = useRef < HTMLDivElement > ( null )
145
-
146
- const [ showOnLeft , setShowOnLeft ] = useState < boolean > ( false )
147
- const [ showOnRight , setShowOnRight ] = useState < boolean > ( true )
148
-
149
- useEffect ( ( ) => {
150
- const onScroll = ( ) => {
151
- const {
152
- scrollWidth = 0 ,
153
- scrollLeft = 0 ,
154
- offsetWidth = 0 ,
155
- } = ref . current || { }
156
- setShowOnLeft ( scrollLeft != 0 )
157
- setShowOnRight ( scrollLeft + offsetWidth != scrollWidth )
158
- }
159
- onScroll ( )
160
- const node = ref . current
161
- node ?. addEventListener ( 'scroll' , onScroll )
162
- return ( ) => {
163
- node ?. removeEventListener ( 'scroll' , onScroll )
164
- }
165
- } , [ ] )
166
-
167
- return (
168
- < ShadowHorizontal showOnLeft = { showOnLeft } showOnRight = { showOnRight } >
169
- < Container ref = { ref } { ...props } >
170
- { children }
171
- </ Container >
172
- </ ShadowHorizontal >
173
- )
174
- }
175
-
176
108
export const ScrollShadower : FC < Props > = ( { children, ...props } ) => {
177
109
const ref = useRef < HTMLDivElement > ( null )
178
110
@@ -185,6 +117,18 @@ export const ScrollShadower: FC<Props> = ({ children, ...props }) => {
185
117
const [ downOffset , setDownOffset ] = useState < number > ( 0 )
186
118
187
119
useEffect ( ( ) => {
120
+ const onResize = ( ) => {
121
+ const {
122
+ offsetWidth = 0 ,
123
+ offsetHeight = 0 ,
124
+ clientHeight = 0 ,
125
+ clientWidth = 0 ,
126
+ } = ref . current || { }
127
+
128
+ setRightOffset ( offsetWidth - clientWidth )
129
+ setDownOffset ( offsetHeight - clientHeight )
130
+ }
131
+
188
132
const onScroll = ( ) => {
189
133
const {
190
134
scrollWidth = 0 ,
@@ -193,35 +137,37 @@ export const ScrollShadower: FC<Props> = ({ children, ...props }) => {
193
137
scrollHeight = 0 ,
194
138
scrollTop = 0 ,
195
139
offsetHeight = 0 ,
196
- clientHeight = 0 ,
197
- clientWidth = 0 ,
198
140
} = ref . current || { }
199
- setRightOffset ( offsetWidth - clientWidth )
200
- setDownOffset ( offsetHeight - clientHeight )
201
141
202
142
setShowOnLeft ( scrollLeft != 0 )
203
143
setShowOnRight ( scrollLeft + offsetWidth + rightOffset < scrollWidth )
204
144
setShowOnTop ( scrollTop > 0 )
205
145
setShowOnBottom ( scrollTop + offsetHeight + downOffset < scrollHeight )
206
146
}
207
147
onScroll ( )
148
+ onResize ( )
149
+
208
150
const node = ref . current
151
+
209
152
node ?. addEventListener ( 'scroll' , onScroll )
153
+ window . addEventListener ( 'resize' , onResize )
210
154
return ( ) => {
211
155
node ?. removeEventListener ( 'scroll' , onScroll )
156
+ window . removeEventListener ( 'resize' , onResize )
212
157
}
158
+ // eslint-disable-next-line react-hooks/exhaustive-deps
213
159
} , [ ] )
214
160
215
161
return (
216
162
< ShadowVertical
217
163
showOnTop = { showOnTop }
218
164
showOnBottom = { showOnBottom }
219
- offsetDown = { downOffset }
165
+ bottomOffset = { downOffset }
220
166
>
221
167
< ShadowHorizontal
222
168
showOnLeft = { showOnLeft }
223
169
showOnRight = { showOnRight }
224
- offsetRight = { rightOffset }
170
+ rightOffset = { rightOffset }
225
171
>
226
172
< Container ref = { ref } { ...props } >
227
173
{ children }
0 commit comments