Files
headlessui/packages/@headlessui-react/playground-utils/resolve-all-examples.ts
T
Robin Malfait 24725216e4 fix: outside click refocus bug (#114)
* add watch script

* make interactions in Vue and React consistent

* re-work focus restoration

When we click outside of the Menu or Listbox, we want to
restore the focus to the Button, *unless* we clicked on/in an element
that is focusable in itself. For example, when the Menu is open and you
click in an input field, the input field should stay focused. We should
also close the Menu itself at this point.

* add examples with multiple elements

* bump dependencies
2020-10-20 15:38:12 +02:00

47 lines
1.0 KiB
TypeScript

import fs from 'fs'
import path from 'path'
export type ExamplesType = {
name: string
path: string
children?: ExamplesType[]
}
export async function resolveAllExamples(...paths: string[]) {
const base = path.resolve(process.cwd(), ...paths)
if (!fs.existsSync(base)) {
return false
}
const files = await fs.promises.readdir(base, { withFileTypes: true })
const items: ExamplesType[] = []
for (let file of files) {
// Skip reserved filenames from Next. E.g.: _app.tsx, _error.tsx
if (file.name.startsWith('_')) {
continue
}
const bucket: ExamplesType = {
name: file.name.replace(/-/g, ' ').replace(/\.tsx?/g, ''),
path: [...paths, file.name]
.join('/')
.replace(/^pages/, '')
.replace(/\.tsx?/g, '')
.replace(/\/+/g, '/'),
}
if (file.isDirectory()) {
const children = await resolveAllExamples(...paths, file.name)
if (children) {
bucket.children = children
}
}
items.push(bucket)
}
return items
}