-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/9 add use resize observer hook #18
Open
larsvanbraam
wants to merge
2
commits into
main
Choose a base branch
from
feature/9-add-use-resize-observer-hook
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,60 @@ | ||
import { Meta } from '@storybook/addon-docs'; | ||
|
||
<Meta | ||
title="useResizeObserver/docs" | ||
/> | ||
|
||
# useResizeObserver | ||
|
||
Please add a description about the `useResizeObserver` hook. | ||
|
||
## Reference | ||
|
||
```ts | ||
function useResizeObserver( | ||
source: DomElementOrRef, | ||
callback: KeyDownCallback, | ||
debounceTime?: number, | ||
): void | ||
``` | ||
|
||
### Parameters | ||
|
||
* `source` - The DOM Element or Ref that you want to observe for resizes. | ||
* `callback` - The function to invoke when the source is resized. | ||
* `debounceTime` - The amount of debounce you want to apply to the callback function. | ||
|
||
### Returns | ||
|
||
* `void` | ||
|
||
## Usage | ||
|
||
```ts | ||
useResizeObserver(refs.someRef, () => { | ||
console.log('someRef was resized!') | ||
}, 100); | ||
```` | ||
|
||
```ts | ||
const Demo = defineComponent({ | ||
name: 'demo', | ||
refs: { | ||
someRef: 'some-ref', | ||
}, | ||
setup({ refs }) { | ||
|
||
// The most basic implementation of watching an element for resizes. | ||
useResizeObserver(refs.someRef, () => { | ||
console.log('someRef was resized') | ||
}); | ||
|
||
// The callback method can easily be debounced to reduce the amount of triggers. | ||
useResizeObserver(refs.someRef, () => { | ||
console.log('someRef was resized.') | ||
}, 500); | ||
|
||
return []; | ||
} | ||
}) | ||
``` |
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,68 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import { bind, computed, defineComponent, ref } from '@muban/muban'; | ||
import type { Story } from '@muban/storybook/types-6-0'; | ||
import { html } from '@muban/template'; | ||
import { useResizeObserver } from './useResizeObserver'; | ||
import { useStorybookLog } from '../hooks/useStorybookLog'; | ||
|
||
export default { | ||
title: 'useResizeObserver', | ||
}; | ||
|
||
export const Demo: Story = () => ({ | ||
component: defineComponent({ | ||
name: 'story', | ||
refs: { | ||
testArea: 'test-area', | ||
label: 'label', | ||
resizeButton: 'resize-button', | ||
}, | ||
setup({ refs }) { | ||
const [logBinding, log] = useStorybookLog(refs.label); | ||
const width = ref(100); | ||
|
||
function onResizeObserverUpdate(): void { | ||
log('resize observer triggered'); | ||
} | ||
|
||
useResizeObserver(refs.testArea, onResizeObserverUpdate, 100); | ||
|
||
return [ | ||
logBinding, | ||
bind(refs.testArea, { | ||
style: { | ||
width: computed(() => `${width.value}%`), | ||
}, | ||
}), | ||
bind(refs.resizeButton, { | ||
click() { | ||
const newWidth = width.value - 25; | ||
width.value = newWidth > 0 ? newWidth : 100; | ||
}, | ||
}), | ||
]; | ||
}, | ||
}), | ||
template: () => html`<div data-component="story"> | ||
<div class="alert alert-primary"> | ||
<h4 class="alert-heading">Instructions!</h4> | ||
<p>Resize the window or click on the resize button to see the events being triggered.</p> | ||
<p class="mb-0"> | ||
Note: The <code>callback</code> is debounced by <u>100ms</u> to avoid it from being called | ||
way too much for the sake of the demo. | ||
</p> | ||
</div> | ||
<div data-ref="label" /> | ||
<div class="card border-dark" data-ref="test-area"> | ||
<div class="card-header">Test Area</div> | ||
<div class="card-body"> | ||
<p> | ||
The resize observer is attached to this element, so it will trigger the callback method if | ||
it's resized. | ||
</p> | ||
<button type="button" data-ref="resize-button" class="btn btn-success">Resize</button> | ||
</div> | ||
</div> | ||
</div>`, | ||
}); | ||
Demo.storyName = 'demo'; |
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,52 @@ | ||
import { createMockElementRef, runComponentSetup } from '@muban/test-utils'; | ||
import { mockResizeObserver } from 'jsdom-testing-mocks'; | ||
import { useResizeObserver } from './useResizeObserver'; | ||
import { timeout } from './useResizeObserver.test.utils'; | ||
|
||
jest.mock('@muban/muban', () => jest.requireActual('@muban/test-utils').getMubanLifecycleMock()); | ||
|
||
const resizeObserver = mockResizeObserver(); | ||
|
||
describe('useResizeObserver', () => { | ||
it('should not crash', () => { | ||
const { ref } = createMockElementRef(); | ||
|
||
runComponentSetup(() => { | ||
useResizeObserver(ref, () => undefined); | ||
}); | ||
}); | ||
|
||
it('should attach a resize observer to a ref', async () => { | ||
const mockHandler = jest.fn(); | ||
const { ref, target } = createMockElementRef(); | ||
|
||
await runComponentSetup( | ||
() => { | ||
useResizeObserver(ref, mockHandler); | ||
}, | ||
async () => { | ||
resizeObserver.resize(target); | ||
await timeout(0); | ||
}, | ||
); | ||
|
||
expect(mockHandler).toBeCalledTimes(1); | ||
}); | ||
|
||
it('should attach a resize observer to a HTML element', async () => { | ||
const mockHandler = jest.fn(); | ||
const { target } = createMockElementRef(); | ||
|
||
await runComponentSetup( | ||
() => { | ||
useResizeObserver(target, mockHandler); | ||
}, | ||
async () => { | ||
resizeObserver.resize(target); | ||
await timeout(0); | ||
}, | ||
); | ||
|
||
expect(mockHandler).toBeCalledTimes(1); | ||
}); | ||
}); |
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,17 @@ | ||
/** | ||
* A helper method that allows you to easily create a timeout with the async/await notation | ||
* | ||
* ```ts | ||
* async function someFunction(){ | ||
* await timeout(100); | ||
* console.log('This is delayed by 100ms'); | ||
* } | ||
* ``` | ||
* | ||
* @param duration The duration that you want to create the timeout for. | ||
*/ | ||
export async function timeout(duration: number): Promise<void> { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, duration); | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
too often