Skip to content

Use set element attribute #31

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ yarn add react-recipes
| 🍣 [`useScript`](./docs/useScript.md) | [loaded, error] | (src) |
| 🍖 [`useSpeechRecognition`](./docs/useSpeechRecognition.md) | { supported, listen, listening, stop } | ({ onEnd }) |
| 🍗 [`useSpeechSynthesis`](./docs/useSpeechSynthesis.md) | { supported, speak, speaking, cancel, voices } | ({ onEnd, onResult }) |
| 🧈 [`useSetElementAttribute`](./docs/useSetElementAttribute.md) | - | ( elemQuery, attributes) |
| 🍏 [`useThrottle`](./docs/useThrottle.md) | throttledValue | (value, ms: 250) |
| 🍷 [`useWhyDidYouUpdate`](./docs/useWhyDidYouUpdate.md) | - | (name, props) |
| 🥖 [`useWindowScroll`](./docs/useWindowScroll.md) | { x, y } | - |
Expand Down
27 changes: 27 additions & 0 deletions docs/useSetElementAttribute.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 🧈 `useSetElementAttribute`

Sets an attribute on a DOM element

## Arguments

- `elemQuery: string`: name of element to query. Ex - 'html', '.class', '#name', etc
- `attributes: Array`: array of objects of attributes to update
{
`key: string`: name of element attribute to update
`value: string`: value of element attribute
}
## Usage

```jsx
import { useSetElementAttribute } from 'react-recipes';

const App = () => {
const [locale] = useLocal();
const priceData = fetchPriceData();

useSetElementAttribute('html', [{ key: 'lang', value: locale }, { key: 'dir', value: 'rtl' }])
useSetElementAttribute('#bookingPrice', [{ key: 'data-price', value: JSON.stringify(priceData) }, { key: 'aria-label', value: 'price-data' }])

return null
}
```
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export { default as useOnClickOutside } from './useOnClickOutside';
export { default as useOnlineStatus } from './useOnlineStatus';
export { default as usePrevious } from './usePrevious';
export { default as useScript } from './useScript';
export { default as useSetElementAttribute } from './useSetElementAttribute';
export { default as useSpeechRecognition } from './useSpeechRecognition';
export { default as useSpeechSynthesis } from './useSpeechSynthesis';
export { default as useThrottle } from './useThrottle';
Expand Down
29 changes: 29 additions & 0 deletions src/useSetElementAttribute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useEffect } from 'react';

const useSetElementAttribute = (elemQuery, attributes) => {
useEffect(() => {
const domElement = document.querySelector(elemQuery);
if (!domElement) {
return;
}

attributes.forEach(({ key, value }) => {
if (domElement.getAttribute(key) !== value) {
domElement.setAttribute(key, value);
}
});
}, [elemQuery, attributes]);
};

// Usage
// const App = () => {
// const [locale] = useLocal();
// const priceData = fetchPriceData();

// useSetElementAttribute('html', [{ key: 'lang', value: locale }, { key: 'dir', value: 'rtl' }])
// useSetElementAttribute('#bookingPrice', [{ key: 'data-price', value: JSON.stringify(priceData) }, { key: 'aria-label', value: 'price-data' }])

// return ...
// }

export default useSetElementAttribute;