-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: text area resizes properly every time it becomes visible (#374)
* fix: text area resizes properly every time it becomes visible Closes #314
- Loading branch information
1 parent
1e5849e
commit b5eeced
Showing
5 changed files
with
185 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useLayoutEffect, useState } from 'preact/hooks'; | ||
|
||
export function useElementVisible(element) { | ||
|
||
const [ visible, setVisible ] = useState(!!element && !!element.clientHeight); | ||
|
||
useLayoutEffect(() => { | ||
if (!element) return; | ||
|
||
const resizeObserver = new ResizeObserver(([ entry ]) => { | ||
requestAnimationFrame(() => { | ||
const newVisible = !!entry.contentRect.height; | ||
if (newVisible !== visible) { | ||
setVisible(newVisible); | ||
} | ||
}); | ||
}); | ||
|
||
resizeObserver.observe(element); | ||
|
||
return () => resizeObserver.disconnect(); | ||
}, [ element, visible ]); | ||
|
||
return visible; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { renderHook } from '@testing-library/preact-hooks'; | ||
|
||
import { waitFor } from '@testing-library/preact'; | ||
|
||
import { useElementVisible } from 'src/hooks'; | ||
|
||
import TestContainer from 'mocha-test-container-support'; | ||
|
||
|
||
describe('hooks/useElementVisible', function() { | ||
|
||
let container; | ||
beforeEach(function() { | ||
container = TestContainer.get(this); | ||
}); | ||
|
||
|
||
it('should return true for visible element', async function() { | ||
|
||
// given | ||
const element = global.document.createElement('div'); | ||
container.appendChild(element); | ||
|
||
// when | ||
const { result } = renderHook(() => useElementVisible(element)); | ||
|
||
// then | ||
expect(result.current).to.be.true; | ||
}); | ||
|
||
|
||
it('should return false for not visible element', async function() { | ||
|
||
// given | ||
const element = global.document.createElement('div'); | ||
|
||
// when | ||
const { result } = renderHook(() => useElementVisible(element)); | ||
|
||
// then | ||
expect(result.current).to.be.false; | ||
}); | ||
|
||
|
||
it('should return false when element does not exist', async function() { | ||
|
||
// given | ||
const element = undefined; | ||
|
||
// when | ||
const { result } = renderHook(() => useElementVisible(element)); | ||
|
||
// then | ||
expect(result.current).to.be.false; | ||
}); | ||
|
||
|
||
it('should return true when element becomes visible', async function() { | ||
|
||
// given | ||
const element = global.document.createElement('div'); | ||
container.appendChild(element); | ||
element.style.display = 'none'; | ||
|
||
// when | ||
const { rerender, result } = renderHook(() => useElementVisible(element)); | ||
|
||
// then | ||
await waitFor(() => { | ||
rerender(); | ||
expect(result.current).to.be.false; | ||
}); | ||
|
||
// when | ||
element.style.display = 'block'; | ||
|
||
// then | ||
await waitFor(() => { | ||
rerender(); | ||
expect(result.current).to.be.true; | ||
}); | ||
}); | ||
|
||
|
||
it('should return false when element becomes hidden', async function() { | ||
|
||
// given | ||
const element = global.document.createElement('div'); | ||
container.appendChild(element); | ||
|
||
// when | ||
const { rerender, result } = renderHook(() => useElementVisible(element)); | ||
|
||
// then | ||
await waitFor(() => { | ||
rerender(); | ||
expect(result.current).to.be.true; | ||
}); | ||
|
||
// when | ||
element.style.display = 'none'; | ||
|
||
// then | ||
await waitFor(() => { | ||
rerender(); | ||
expect(result.current).to.be.false; | ||
}); | ||
}); | ||
}); |