@@ -15,18 +15,28 @@ See the License for the specific language governing permissions and
1515limitations under the License.
1616*/
1717
18- import React , { HTMLAttributes , WheelEvent } from "react" ;
18+ import classNames from "classnames" ;
19+ import React , { HTMLAttributes , ReactHTML , WheelEvent } from "react" ;
1920
20- interface IProps extends Omit < HTMLAttributes < HTMLDivElement > , "onScroll" > {
21+ type DynamicHtmlElementProps < T extends keyof JSX . IntrinsicElements > =
22+ JSX . IntrinsicElements [ T ] extends HTMLAttributes < { } > ? DynamicElementProps < T > : DynamicElementProps < "div" > ;
23+ type DynamicElementProps < T extends keyof JSX . IntrinsicElements > = Partial < Omit < JSX . IntrinsicElements [ T ] , 'ref' > > ;
24+
25+ export type IProps < T extends keyof JSX . IntrinsicElements > = DynamicHtmlElementProps < T > & {
26+ element ?: T ;
2127 className ?: string ;
2228 onScroll ?: ( event : Event ) => void ;
2329 onWheel ?: ( event : WheelEvent ) => void ;
2430 style ?: React . CSSProperties ;
2531 tabIndex ?: number ;
2632 wrappedRef ?: ( ref : HTMLDivElement ) => void ;
27- }
33+ } ;
34+
35+ export default class AutoHideScrollbar < T extends keyof JSX . IntrinsicElements > extends React . Component < IProps < T > > {
36+ static defaultProps = {
37+ element : 'div' as keyof ReactHTML ,
38+ } ;
2839
29- export default class AutoHideScrollbar extends React . Component < IProps > {
3040 public readonly containerRef : React . RefObject < HTMLDivElement > = React . createRef ( ) ;
3141
3242 public componentDidMount ( ) {
@@ -36,9 +46,7 @@ export default class AutoHideScrollbar extends React.Component<IProps> {
3646 this . containerRef . current . addEventListener ( "scroll" , this . props . onScroll , { passive : true } ) ;
3747 }
3848
39- if ( this . props . wrappedRef ) {
40- this . props . wrappedRef ( this . containerRef . current ) ;
41- }
49+ this . props . wrappedRef ?.( this . containerRef . current ) ;
4250 }
4351
4452 public componentWillUnmount ( ) {
@@ -49,19 +57,15 @@ export default class AutoHideScrollbar extends React.Component<IProps> {
4957
5058 public render ( ) {
5159 // eslint-disable-next-line @typescript-eslint/no-unused-vars
52- const { className , onScroll , onWheel , style , tabIndex, wrappedRef, children, ...otherProps } = this . props ;
60+ const { element , className , onScroll , tabIndex, wrappedRef, children, ...otherProps } = this . props ;
5361
54- return ( < div
55- { ...otherProps }
56- ref = { this . containerRef }
57- style = { style }
58- className = { [ "mx_AutoHideScrollbar" , className ] . join ( " " ) }
59- onWheel = { onWheel }
62+ return React . createElement ( element , {
63+ ...otherProps ,
64+ ref : this . containerRef ,
65+ className : classNames ( "mx_AutoHideScrollbar" , className ) ,
6066 // Firefox sometimes makes this element focusable due to
6167 // overflow:scroll;, so force it out of tab order by default.
62- tabIndex = { tabIndex ?? - 1 }
63- >
64- { children }
65- </ div > ) ;
68+ tabIndex : tabIndex ?? - 1 ,
69+ } , children ) ;
6670 }
6771}
0 commit comments