@@ -4,32 +4,31 @@ import styled, { css } from 'styled-components'
44interface Props {
55 height : string
66 width : string
7- color : string
87}
98
109interface HorizontalShadowProps {
1110 showOnLeft : boolean
1211 showOnRight : boolean
13- offsetRight ?: number
12+ rightOffset ?: number
1413}
1514
1615interface VerticalShadowProps {
1716 showOnTop : boolean
1817 showOnBottom : boolean
19- offsetDown ?: number
18+ bottomOffset ?: number
2019}
2120
2221const ShadowHorizontal = styled . div < HorizontalShadowProps > `
2322 position: relative;
24- ${ props =>
25- props . showOnRight
23+ ${ ( { showOnRight , rightOffset = 0 } ) =>
24+ showOnRight
2625 ? css `
2726 ::after {
2827 content : '' ;
2928 position : absolute;
3029 top : 0 ;
3130 bottom : 0 ;
32- right : ${ props . offsetRight || 0 } px;
31+ right : ${ rightOffset } px;
3332 width : 30px ;
3433 background : linear-gradient (
3534 to right,
@@ -39,8 +38,8 @@ const ShadowHorizontal = styled.div<HorizontalShadowProps>`
3938 }
4039 `
4140 : '' }
42- ${ props =>
43- props . showOnLeft
41+ ${ ( { showOnLeft } ) =>
42+ showOnLeft
4443 ? css `
4544 ::before {
4645 content : '' ;
@@ -61,8 +60,8 @@ const ShadowHorizontal = styled.div<HorizontalShadowProps>`
6160
6261const ShadowVertical = styled . div < VerticalShadowProps > `
6362 position: relative;
64- ${ props =>
65- props . showOnTop
63+ ${ ( { showOnTop } ) =>
64+ showOnTop
6665 ? css `
6766 ::before {
6867 content : '' ;
@@ -79,13 +78,13 @@ const ShadowVertical = styled.div<VerticalShadowProps>`
7978 }
8079 `
8180 : '' }
82- ${ props =>
83- props . showOnBottom
81+ ${ ( { showOnBottom , bottomOffset = 0 } ) =>
82+ showOnBottom
8483 ? css `
8584 ::after {
8685 content : '' ;
8786 position : absolute;
88- bottom : ${ props . offsetDown || 0 } px;
87+ bottom : ${ bottomOffset } px;
8988 left : 0 ;
9089 right : 0 ;
9190 height : 30px ;
@@ -106,73 +105,6 @@ const Container = styled.div<Props>`
106105 display: flex;
107106`
108107
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-
176108export const ScrollShadower : FC < Props > = ( { children, ...props } ) => {
177109 const ref = useRef < HTMLDivElement > ( null )
178110
@@ -185,6 +117,18 @@ export const ScrollShadower: FC<Props> = ({ children, ...props }) => {
185117 const [ downOffset , setDownOffset ] = useState < number > ( 0 )
186118
187119 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+
188132 const onScroll = ( ) => {
189133 const {
190134 scrollWidth = 0 ,
@@ -193,35 +137,37 @@ export const ScrollShadower: FC<Props> = ({ children, ...props }) => {
193137 scrollHeight = 0 ,
194138 scrollTop = 0 ,
195139 offsetHeight = 0 ,
196- clientHeight = 0 ,
197- clientWidth = 0 ,
198140 } = ref . current || { }
199- setRightOffset ( offsetWidth - clientWidth )
200- setDownOffset ( offsetHeight - clientHeight )
201141
202142 setShowOnLeft ( scrollLeft != 0 )
203143 setShowOnRight ( scrollLeft + offsetWidth + rightOffset < scrollWidth )
204144 setShowOnTop ( scrollTop > 0 )
205145 setShowOnBottom ( scrollTop + offsetHeight + downOffset < scrollHeight )
206146 }
207147 onScroll ( )
148+ onResize ( )
149+
208150 const node = ref . current
151+
209152 node ?. addEventListener ( 'scroll' , onScroll )
153+ window . addEventListener ( 'resize' , onResize )
210154 return ( ) => {
211155 node ?. removeEventListener ( 'scroll' , onScroll )
156+ window . removeEventListener ( 'resize' , onResize )
212157 }
158+ // eslint-disable-next-line react-hooks/exhaustive-deps
213159 } , [ ] )
214160
215161 return (
216162 < ShadowVertical
217163 showOnTop = { showOnTop }
218164 showOnBottom = { showOnBottom }
219- offsetDown = { downOffset }
165+ bottomOffset = { downOffset }
220166 >
221167 < ShadowHorizontal
222168 showOnLeft = { showOnLeft }
223169 showOnRight = { showOnRight }
224- offsetRight = { rightOffset }
170+ rightOffset = { rightOffset }
225171 >
226172 < Container ref = { ref } { ...props } >
227173 { children }
0 commit comments