Skip to content

Commit

Permalink
fix(ref): checks if ref exists and also it passes the node dom (#31)
Browse files Browse the repository at this point in the history
closes #30
  • Loading branch information
danilowoz committed Apr 13, 2020
1 parent 430090b commit f0b5d00
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ const Component = () => {
const snuggleRef = useRef()

const onLoad = () => {
snuggleRef.current.resize()
if (snuggleRef.current) {
snuggleRef.current.resize()
}
}

return (
Expand Down
11 changes: 8 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class Snuggle extends React.PureComponent<ISnuggle> {
super(props)

this.gridId = `snuggle--${props.uniqueid || key()}`
props.innerRef.current = { resize: this.setValues }
}

componentDidMount() {
Expand Down Expand Up @@ -113,6 +112,7 @@ class Snuggle extends React.PureComponent<ISnuggle> {
children,
item = React.createElement('div'),
container = React.createElement('div'),
innerRef,
...compProps
} = this.props

Expand All @@ -126,8 +126,13 @@ class Snuggle extends React.PureComponent<ISnuggle> {
this.getRef(n)
}

const refGrid = (n: HTMLElement): void => {
this.grid = n
const refGrid = (node: HTMLElement): void => {
this.grid = node

if (innerRef) {
innerRef.current = node
innerRef.current.resize = this.setValues
}
}

const renderChildren = React.Children.map(
Expand Down
30 changes: 28 additions & 2 deletions stories/index.stories.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useRef, useEffect } from 'react'
import { storiesOf } from '@storybook/react'
import { withKnobs, text, boolean, number } from '@storybook/addon-knobs'

Expand All @@ -15,7 +15,7 @@ import {
storiesOf('Snuggle', module)
.add('default', () => (
<div className="wrap">
<Snuggle item={<div className="card" />}>{listElements()}</Snuggle>
<Snuggle>{listElements()}</Snuggle>
</div>
))

Expand All @@ -33,6 +33,30 @@ storiesOf('Snuggle', module)

.add('on update grid', () => <OnUpdateGrid />)

const ResizeElements = () => {
const ref = useRef()

useEffect(() => {
const setResize = () => {
if (ref.current && ref.current.resize) {
ref.current.resize()
}
}

window.addEventListener('resize', setResize)

return () => {
window.removeEventListener('resize', setResize)
}
}, [])

return (
<div className="wrap">
<Snuggle ref={ref}>{listElements()}</Snuggle>
</div>
)
}

storiesOf('Options', module)
.addDecorator(withKnobs)

Expand Down Expand Up @@ -88,6 +112,8 @@ storiesOf('Options', module)
</div>
))

.add('on resize method', () => <ResizeElements />)

storiesOf('Third party dependencies', module).add('with scroll reveal', () => (
<RevealAnimation />
))

0 comments on commit f0b5d00

Please sign in to comment.