diff --git a/app/routes/circulars.$circularId.($version)/route.tsx b/app/routes/circulars.$circularId.($version)/route.tsx index d3e9ed1ff..715fe12dd 100644 --- a/app/routes/circulars.$circularId.($version)/route.tsx +++ b/app/routes/circulars.$circularId.($version)/route.tsx @@ -9,8 +9,9 @@ import type { HeadersFunction, LoaderFunctionArgs } from '@remix-run/node' import { json } from '@remix-run/node' import { Link, useLoaderData, useRouteLoaderData } from '@remix-run/react' import { Button, ButtonGroup, CardBody, Icon } from '@trussworks/react-uswds' -import { useState } from 'react' +import { useRef, useState } from 'react' import invariant from 'tiny-invariant' +import { useOnClickOutside } from 'usehooks-ts' import type { loader as parentLoader } from '../circulars.$circularId/route' import { get } from '../circulars/circulars.server' @@ -154,23 +155,26 @@ function CircularsHistory({ circular: number history?: number[] }) { - const [showVersions, setShowVersions] = useState(false) - + const ref = useRef(null) + const [showContent, setShowContent] = useState(false) + useOnClickOutside(ref, () => { + setShowContent(false) + }) return ( - <> +
{ - setShowVersions((shown) => !shown) + setShowContent((shown) => !shown) }} > Versions - {showVersions && ( + {showContent && ( setShowVersions(!showVersions)} + onClick={() => setShowContent(!showContent)} to={`/circulars/${circular}`} > Latest @@ -179,7 +183,7 @@ function CircularsHistory({ history.map((version) => (
setShowVersions(!showVersions)} + onClick={() => setShowContent(!showContent)} to={`/circulars/${circular}/${version}`} > Version {version} @@ -189,6 +193,6 @@ function CircularsHistory({ )} - +
) } diff --git a/app/routes/docs_._schema-browser.schema.($version).$/route.tsx b/app/routes/docs_._schema-browser.schema.($version).$/route.tsx index f33ad2522..e40ad6c4f 100644 --- a/app/routes/docs_._schema-browser.schema.($version).$/route.tsx +++ b/app/routes/docs_._schema-browser.schema.($version).$/route.tsx @@ -18,9 +18,10 @@ import { } from '@trussworks/react-uswds' import classNames from 'classnames' import { dirname } from 'path' -import { useState } from 'react' +import { useRef, useState } from 'react' import { useParams } from 'react-router' import invariant from 'tiny-invariant' +import { useOnClickOutside } from 'usehooks-ts' import type { loader as parentLoader } from '../docs_._schema-browser' import type { Schema } from './components' @@ -125,19 +126,23 @@ function VersionSelector({ versions: { name: string | null; ref: string }[] path: string }) { - const [showVersions, setShowVersions] = useState(false) + const ref = useRef(null) + const [showContent, setShowContent] = useState(false) + useOnClickOutside(ref, () => { + setShowContent(false) + }) return ( - <> +
{ - setShowVersions((shown) => !shown) + setShowContent((shown) => !shown) }} > Version: {version} - {showVersions && ( + {showContent && (

Versions

@@ -149,7 +154,7 @@ function VersionSelector({ className="usa-link" to={`/docs/schema/${ref}/${path}`} onClick={() => { - setShowVersions(false) + setShowContent(false) }} > {name || ref} @@ -159,7 +164,7 @@ function VersionSelector({
)} - +
) }