Skip to content

Commit

Permalink
Support forwardRef.
Browse files Browse the repository at this point in the history
Signed-off-by: Charlie Hess <[email protected]>
  • Loading branch information
CharlieHess committed Apr 1, 2021
1 parent f47635d commit 94c2e74
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
"sourceType": "module"
},
"plugins": ["react", "@typescript-eslint"],
"rules": {},
"rules": {
"react/display-name": 0,
"react/prop-types": 0
},
"settings": {
"react": {
"version": "detect"
Expand Down
37 changes: 19 additions & 18 deletions packages/react-div-100vh/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import React, { useState, useEffect, HTMLAttributes } from 'react'
import React, { forwardRef, useState, useEffect, HTMLAttributes } from 'react'

let warned = false

export default function Div100vh({
style = {},
...other
}: HTMLAttributes<HTMLDivElement>): JSX.Element {
const height = use100vh()
const Div100vh = forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>(
({ style, ...other }, ref) => {
const height = use100vh()

// TODO: warn only in development
if (!warned && style.height) {
warned = true
console.warn(
'<ReactDiv100vh /> overrides the height property of the style prop'
)
}
const styleWithRealHeight = {
...style,
height: height ? `${height}px` : '100vh'
// TODO: warn only in development
if (!warned && style?.height) {
warned = true
console.warn(
'<ReactDiv100vh /> overrides the height property of the style prop'
)
}
const styleWithRealHeight = {
...style,
height: height ? `${height}px` : '100vh'
}
return <div ref={ref} style={styleWithRealHeight} {...other} />
}
return <div style={styleWithRealHeight} {...other} />
}
)

export default Div100vh

export function use100vh(): number | null {
const [height, setHeight] = useState<number | null>(measureHeight)
Expand Down

0 comments on commit 94c2e74

Please sign in to comment.