-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Add use-mutation-observer hook
- Loading branch information
Showing
9 changed files
with
222 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,4 +1,17 @@ | ||
import { HooksDemos, ScatterChartDemos } from '@docs/demos'; | ||
import { Layout } from '@/layout'; | ||
import { MDX_DATA } from '@/mdx'; | ||
|
||
export default Layout(MDX_DATA.Changelog770); | ||
|
||
## ScatterChart component | ||
|
||
New [ScatterChart](/charts/scatter-chart) component: | ||
|
||
<Demo data={ScatterChartDemos.usage} /> | ||
|
||
## use-mutation-observer hook | ||
|
||
New [useMutationObserver](/hooks/use-mutation-observer) hook: | ||
|
||
<Demo data={HooksDemos.useMutationObserverTarget} /> |
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,29 @@ | ||
import { HooksDemos } from '@docs/demos'; | ||
import { Layout } from '@/layout'; | ||
import { MDX_DATA } from '@/mdx'; | ||
|
||
export default Layout(MDX_DATA.useMutationObserver); | ||
|
||
## Usage | ||
|
||
`use-mutation-observer` is a wrapper for the [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver). | ||
It allows subscribing changes being made to the DOM tree. | ||
|
||
<Demo data={HooksDemos.useMutationObserverUsage} /> | ||
|
||
## Target element | ||
|
||
If you cannot pass `ref` to the target element, you can pass a function to resolve | ||
the target element as a third argument. | ||
|
||
<Demo data={HooksDemos.useMutationObserverTarget} /> | ||
|
||
## Definition | ||
|
||
```tsx | ||
function useMutationObserver<Element extends HTMLElement>( | ||
callback: MutationCallback, | ||
options: MutationObserverInit, | ||
target?: HTMLElement | (() => HTMLElement) | null | ||
): RefObject<Element>; | ||
``` |
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
76 changes: 76 additions & 0 deletions
76
packages/@docs/demos/src/demos/hooks/use-mutation-observer.demo.target.tsx
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,76 @@ | ||
import React, { useState } from 'react'; | ||
import { Kbd, Text } from '@mantine/core'; | ||
import { useMutationObserver } from '@mantine/hooks'; | ||
import { MantineDemo } from '@mantinex/demo'; | ||
|
||
const code = ` | ||
import { useState } from 'react'; | ||
import { Kbd, Text } from '@mantine/core'; | ||
import { useMutationObserver } from '@mantine/hooks'; | ||
function Demo() { | ||
const [lastMutation, setLastMutation] = useState(''); | ||
useMutationObserver( | ||
(mutations) => { | ||
mutations.forEach((mutation) => { | ||
if (mutation.type === 'attributes' && mutation.attributeName === 'dir') { | ||
mutation.target instanceof HTMLElement && | ||
setLastMutation(mutation.target.getAttribute('dir') || ''); | ||
} | ||
}); | ||
}, | ||
{ | ||
attributes: true, | ||
attributeFilter: ['dir'], | ||
}, | ||
() => document.documentElement | ||
); | ||
return ( | ||
<> | ||
<Text> | ||
Press <Kbd>Ctrl</Kbd> + <Kbd>Shift</Kbd> + <Kbd>L</Kbd> to change direction | ||
</Text> | ||
<Text mt={10}>Direction was changed to: {lastMutation || 'Not changed yet'}</Text> | ||
</> | ||
); | ||
} | ||
`; | ||
|
||
function Demo() { | ||
const [lastMutation, setLastMutation] = useState(''); | ||
|
||
useMutationObserver( | ||
(mutations) => { | ||
mutations.forEach((mutation) => { | ||
if (mutation.type === 'attributes' && mutation.attributeName === 'dir') { | ||
mutation.target instanceof HTMLElement && | ||
setLastMutation(mutation.target.getAttribute('dir') || ''); | ||
} | ||
}); | ||
}, | ||
{ | ||
attributes: true, | ||
attributeFilter: ['dir'], | ||
}, | ||
() => document.documentElement | ||
); | ||
|
||
return ( | ||
<> | ||
<Text> | ||
Press <Kbd>Ctrl</Kbd> + <Kbd>Shift</Kbd> + <Kbd>L</Kbd> to change direction | ||
</Text> | ||
|
||
<Text mt={10}>Direction was changed to: {lastMutation || 'Not changed yet'}</Text> | ||
</> | ||
); | ||
} | ||
|
||
export const useMutationObserverTarget: MantineDemo = { | ||
type: 'code', | ||
component: Demo, | ||
code, | ||
}; |
86 changes: 86 additions & 0 deletions
86
packages/@docs/demos/src/demos/hooks/use-mutation-observer.demo.usage.tsx
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,86 @@ | ||
import React, { useState } from 'react'; | ||
import { Button, Text } from '@mantine/core'; | ||
import { useMutationObserver } from '@mantine/hooks'; | ||
import { MantineDemo } from '@mantinex/demo'; | ||
|
||
const code = ` | ||
import { useState } from 'react'; | ||
import { Button, Text } from '@mantine/core'; | ||
import { useMutationObserver } from '@mantine/hooks'; | ||
function Demo() { | ||
const [lastMutation, setLastMutation] = useState(''); | ||
const ref = useMutationObserver<HTMLButtonElement>( | ||
(mutations) => { | ||
mutations.forEach((mutation) => { | ||
if (mutation.type === 'attributes' && mutation.attributeName === 'data-mutation') { | ||
mutation.target instanceof HTMLElement && | ||
setLastMutation(mutation.target.dataset.mutation || ''); | ||
} | ||
}); | ||
}, | ||
{ | ||
attributes: true, | ||
attributeFilter: ['data-mutation'], | ||
} | ||
); | ||
return ( | ||
<> | ||
<Button | ||
ref={ref} | ||
onClick={(event) => { | ||
event.currentTarget.dataset.mutation = Math.random().toFixed(3); | ||
}} | ||
> | ||
Click to change to data-mutation attribute | ||
</Button> | ||
<Text mt={10} size="sm"> | ||
Last detected mutation: {lastMutation || 'Not mutated yet'} | ||
</Text> | ||
</> | ||
); | ||
} | ||
`; | ||
|
||
function Demo() { | ||
const [lastMutation, setLastMutation] = useState(''); | ||
|
||
const ref = useMutationObserver<HTMLButtonElement>( | ||
(mutations) => { | ||
mutations.forEach((mutation) => { | ||
if (mutation.type === 'attributes' && mutation.attributeName === 'dir') { | ||
mutation.target instanceof HTMLElement && | ||
setLastMutation(mutation.target.dataset.mutation || ''); | ||
} | ||
}); | ||
}, | ||
{ | ||
attributes: true, | ||
attributeFilter: ['data-mutation'], | ||
} | ||
); | ||
|
||
return ( | ||
<> | ||
<Button | ||
ref={ref} | ||
onClick={(event) => { | ||
event.currentTarget.dataset.mutation = Math.random().toFixed(3); | ||
}} | ||
> | ||
Click to change to data-mutation attribute | ||
</Button> | ||
<Text mt={10} size="sm"> | ||
Last detected mutation: {lastMutation || 'Not mutated yet'} | ||
</Text> | ||
</> | ||
); | ||
} | ||
|
||
export const useMutationObserverUsage: MantineDemo = { | ||
type: 'code', | ||
component: Demo, | ||
code, | ||
}; |
2 changes: 1 addition & 1 deletion
2
packages/@mantine/hooks/src/use-mutation-observer/use-mutation-observer.ts
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