Skip to content

Commit ef00732

Browse files
authored
cleanup and consistency (#213)
- Made the use of `const` and `let` consistent - import required functions and types from 'react' instead of using the `React.` namespace. - Added `Expand` type, which can expand complex types to their "final" result. - Ensured that we use `as const` for DEFAULT_XXX_TAG where we used a string. So that we have the type of `div` instead of `string` for example. - Used `interface` over `type` where possible. I'm personally more of a `type` fan. But the TypeScript recommends `interfaces` where possible because they are faster, yield better error messages and so on.
1 parent da179ca commit ef00732

File tree

78 files changed

+1119
-1053
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+1119
-1053
lines changed

jest/custom-matchers.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import '@testing-library/jest-dom/extend-expect'
22

33
// Assuming requestAnimationFrame is roughly 60 frames per second
4-
const frame = 1000 / 60
5-
const amountOfFrames = 2
4+
let frame = 1000 / 60
5+
let amountOfFrames = 2
66

7-
const formatter = new Intl.NumberFormat('en')
7+
let formatter = new Intl.NumberFormat('en')
88

99
expect.extend({
1010
toBeWithinRenderFrame(actual, expected) {
11-
const min = expected - frame * amountOfFrames
12-
const max = expected + frame * amountOfFrames
11+
let min = expected - frame * amountOfFrames
12+
let max = expected + frame * amountOfFrames
1313

14-
const pass = actual >= min && actual <= max
14+
let pass = actual >= min && actual <= max
1515

1616
return {
1717
message: pass

packages/@headlessui-react/pages/_app.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, { useState, useEffect } from 'react'
22
import Link from 'next/link'
33
import Head from 'next/head'
44

@@ -7,7 +7,7 @@ import { useDisposables } from '../src/hooks/use-disposables'
77
import { PropsOf } from '../src/types'
88

99
function NextLink(props: PropsOf<'a'>) {
10-
const { href, children, ...rest } = props
10+
let { href, children, ...rest } = props
1111
return (
1212
<Link href={href}>
1313
<a {...rest}>{children}</a>
@@ -58,23 +58,23 @@ function tap<T>(value: T, cb: (value: T) => void) {
5858
}
5959

6060
function useKeyDisplay() {
61-
const [mounted, setMounted] = React.useState(false)
61+
let [mounted, setMounted] = useState(false)
6262

63-
React.useEffect(() => {
63+
useEffect(() => {
6464
setMounted(true)
6565
}, [])
6666

6767
if (!mounted) return {}
68-
const isMac = navigator.userAgent.indexOf('Mac OS X') !== -1
68+
let isMac = navigator.userAgent.indexOf('Mac OS X') !== -1
6969
return isMac ? KeyDisplayMac : KeyDisplayWindows
7070
}
7171

7272
function KeyCaster() {
73-
const [keys, setKeys] = React.useState<string[]>([])
74-
const d = useDisposables()
75-
const KeyDisplay = useKeyDisplay()
73+
let [keys, setKeys] = useState<string[]>([])
74+
let d = useDisposables()
75+
let KeyDisplay = useKeyDisplay()
7676

77-
React.useEffect(() => {
77+
useEffect(() => {
7878
function handler(event: KeyboardEvent) {
7979
setKeys(current => [
8080
event.shiftKey && event.key !== 'Shift'

packages/@headlessui-react/pages/_error.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import * as React from 'react'
2-
import Error from 'next/error'
1+
import React from 'react'
2+
import ErrorPage from 'next/error'
33
import Head from 'next/head'
44
import Link from 'next/link'
55

66
import { ExamplesType, resolveAllExamples } from '../playground-utils/resolve-all-examples'
77
import { PropsOf } from '../src/types'
88

99
function NextLink(props: PropsOf<'a'>) {
10-
const { href, children, ...rest } = props
10+
let { href, children, ...rest } = props
1111
return (
1212
<Link href={href}>
1313
<a {...rest}>{children}</a>
@@ -25,7 +25,7 @@ export async function getStaticProps() {
2525

2626
export default function Page(props: { examples: false | ExamplesType[] }) {
2727
if (props.examples === false) {
28-
return <Error statusCode={404} />
28+
return <ErrorPage statusCode={404} />
2929
}
3030

3131
return (

packages/@headlessui-react/pages/listbox/listbox-with-pure-tailwind.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import * as React from 'react'
1+
import React, { useState, useEffect } from 'react'
22
import { Listbox } from '@headlessui/react'
33

44
import { classNames } from '../../src/utils/class-names'
55

6-
const people = [
6+
let people = [
77
'Wade Cooper',
88
'Arlene Mccoy',
99
'Devon Webb',
@@ -17,10 +17,10 @@ const people = [
1717
]
1818

1919
export default function Home() {
20-
const [active, setActivePerson] = React.useState(people[2])
20+
let [active, setActivePerson] = useState(people[2])
2121

2222
// Choose a random person on mount
23-
React.useEffect(() => {
23+
useEffect(() => {
2424
setActivePerson(people[Math.floor(Math.random() * people.length)])
2525
}, [])
2626

packages/@headlessui-react/pages/listbox/multiple-elements.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import * as React from 'react'
1+
import React, { useState, useEffect } from 'react'
22
import { Listbox } from '@headlessui/react'
33

44
function classNames(...classes) {
55
return classes.filter(Boolean).join(' ')
66
}
77

8-
const people = [
8+
let people = [
99
'Wade Cooper',
1010
'Arlene Mccoy',
1111
'Devon Webb',
@@ -41,10 +41,10 @@ export default function Home() {
4141
}
4242

4343
function PeopleList() {
44-
const [active, setActivePerson] = React.useState(people[2])
44+
let [active, setActivePerson] = useState(people[2])
4545

4646
// Choose a random person on mount
47-
React.useEffect(() => {
47+
useEffect(() => {
4848
setActivePerson(people[Math.floor(Math.random() * people.length)])
4949
}, [])
5050

packages/@headlessui-react/pages/menu/menu-with-framer-motion.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as React from 'react'
1+
import React from 'react'
22
import Link from 'next/link'
33
import { AnimatePresence, motion } from 'framer-motion'
44
import { Menu } from '../../src/components/menu/menu'
@@ -69,7 +69,7 @@ export default function Home() {
6969
}
7070

7171
function NextLink(props: PropsOf<'a'>) {
72-
const { href, children, ...rest } = props
72+
let { href, children, ...rest } = props
7373
return (
7474
<Link href={href}>
7575
<a {...rest}>{children}</a>

packages/@headlessui-react/pages/menu/menu-with-popper.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as React from 'react'
2-
import * as ReactDOM from 'react-dom'
1+
import React, { ReactNode, useState, useEffect } from 'react'
2+
import { createPortal } from 'react-dom'
33
import { Menu } from '@headlessui/react'
44

55
import { usePopper } from '../../playground-utils/hooks/use-popper'
@@ -9,7 +9,7 @@ function classNames(...classes) {
99
}
1010

1111
export default function Home() {
12-
const [trigger, container] = usePopper({
12+
let [trigger, container] = usePopper({
1313
placement: 'bottom-end',
1414
strategy: 'fixed',
1515
modifiers: [{ name: 'offset', options: { offset: [0, 10] } }],
@@ -87,12 +87,12 @@ export default function Home() {
8787
)
8888
}
8989

90-
function Portal(props: { children: React.ReactNode }) {
91-
const { children } = props
92-
const [mounted, setMounted] = React.useState(false)
90+
function Portal(props: { children: ReactNode }) {
91+
let { children } = props
92+
let [mounted, setMounted] = useState(false)
9393

94-
React.useEffect(() => setMounted(true), [])
94+
useEffect(() => setMounted(true), [])
9595

9696
if (!mounted) return null
97-
return ReactDOM.createPortal(children, document.body)
97+
return createPortal(children, document.body)
9898
}

packages/@headlessui-react/pages/menu/menu-with-transition-and-popper.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as React from 'react'
1+
import React from 'react'
22
import { Menu, Transition } from '@headlessui/react'
33

44
import { usePopper } from '../../playground-utils/hooks/use-popper'
@@ -8,7 +8,7 @@ function classNames(...classes) {
88
}
99

1010
export default function Home() {
11-
const [trigger, container] = usePopper({
11+
let [trigger, container] = usePopper({
1212
placement: 'bottom-end',
1313
strategy: 'fixed',
1414
modifiers: [{ name: 'offset', options: { offset: [0, 10] } }],

packages/@headlessui-react/pages/menu/menu-with-transition.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as React from 'react'
1+
import React from 'react'
22
import { Menu, Transition } from '@headlessui/react'
33

44
function classNames(...classes) {

packages/@headlessui-react/pages/menu/menu.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as React from 'react'
1+
import React from 'react'
22
import { Menu } from '@headlessui/react'
33

44
import { PropsOf } from '../../src/types'
@@ -55,7 +55,7 @@ export default function Home() {
5555
)
5656
}
5757

58-
function CustomMenuItem(props: PropsOf<Menu.Item<'a'>>) {
58+
function CustomMenuItem(props: PropsOf<typeof Menu.Item>) {
5959
return (
6060
<Menu.Item {...props}>
6161
{({ active, disabled }) => (

0 commit comments

Comments
 (0)