Skip to content
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:** TopbarSelect hooks #943

Merged
merged 14 commits into from
Mar 11, 2019
18 changes: 4 additions & 14 deletions src/TopbarSelect/TopbarSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import React, { useRef, useState } from "react"
import React, { useLayoutEffect, useRef, useState } from "react"

import ContextMenu, { ContextMenuProps } from "../ContextMenu/ContextMenu"
import Icon from "../Icon/Icon"
import useDebouncedCallback from "../useDebouncedCallback"
import useWindowEventListener from "../useWindowEventListener"
import styled from "../utils/styled"

export interface TopbarSelectProps {
Expand Down Expand Up @@ -66,19 +64,11 @@ const TopbarSelect = ({ label, selected, items, onChange, ...props }: TopbarSele

const containerRef = useRef<HTMLDivElement>(null)

const updateContainerWidth = () => {
if (!containerRef.current) {
return
}

if (containerRef.current.clientWidth !== containerWidth) {
useLayoutEffect(() => {
if (containerRef.current) {
setContainerWidth(containerRef.current.clientWidth)
}
}

const debouncedUpdateRenderedWidth = useDebouncedCallback(updateContainerWidth, 100, [])

useWindowEventListener("resize", debouncedUpdateRenderedWidth)
})

return (
<ContextMenu
Expand Down
18 changes: 14 additions & 4 deletions src/useWindowEventListener/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
## Usage

TODO
This hook is used to attach a callback to a window event.

```jsx
const MyComponent = () => {
return <>TODO</>
const Example = () => {
const [pressedKeys, setKeys] = useState([])
useWindowEventListener("keypress", ev => {
setKeys([...keys, ev.key])
})

return (
<div>
<strong>Press any keys: </strong>
<output>{pressedKeys.join(" > ")}</output>
</div>
)
}

;<MyComponent />
;<Example />
```
9 changes: 5 additions & 4 deletions src/useWindowEventListener/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { useEffect } from "react"
/**
* Hook version of window.addEventListener.
*/
function useWindowEventListener<K extends keyof GlobalEventHandlersEventMap>(

function useWindowEventListener<K extends keyof WindowEventMap>(
type: K,
listener: EventListenerOrEventListenerObject,
addOptions?: AddEventListenerOptions,
removeOptions?: EventListenerOptions,
listener: (ev: WindowEventMap[K]) => any,
JoshRosenstein marked this conversation as resolved.
Show resolved Hide resolved
addOptions?: boolean | AddEventListenerOptions,
removeOptions?: boolean | EventListenerOptions,
TejasQ marked this conversation as resolved.
Show resolved Hide resolved
) {
useEffect(() => {
window.addEventListener(type, listener, addOptions)
Expand Down