File size: 7,108 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
import { useRef, useState } from 'react'
import { MultiValue } from 'react-select'
import {
ActionFunction,
json,
LoaderFunction,
MetaFunction,
redirect,
} from '@vercel/remix'
import {
useLoaderData,
Form,
useFetcher,
useNavigation,
useSearchParams,
} from '@remix-run/react'
import { Header } from '~/components/Header/Header'
import { CardExample } from '~/components/Cards/CardExample'
import { Heading } from '~/components/Text/Heading'
import { Copy } from '~/components/Text/Copy'
import { Anchor } from '~/components/Text/Anchor'
import { Select } from '~/components/Select'
import { SANDBOXES } from '~/data/sandboxes'
import { fetchSandbox, getTagsAndComponents } from '~/helpers/sandboxes'
import { WidgetCarbon } from '../components/Widgets/WidgetCarbon'
import {
copy,
exampleFilters,
flex,
h2,
main,
sandboxesList,
xlHeading,
} from '../styles/routes/examples.css'
export const loader: LoaderFunction = async ({ request }) => {
try {
const url = new URL(request.url)
const tagsParam = url.searchParams.get('tags')?.split(',') ?? []
const componentsParam = url.searchParams.get('components')?.split(',') ?? []
const sandboxes = await Promise.all(
Object.values(SANDBOXES).map(fetchSandbox)
).then(boxes => boxes.sort((a, b) => a.title.localeCompare(b.title)))
const filteredSandboxes = sandboxes.filter(sandbox => {
if (tagsParam.length === 0 && componentsParam.length === 0) {
return sandbox
}
const tags = sandbox.tags.filter(tag => tagsParam.includes(tag))
const components = sandbox.tags.filter(tag =>
componentsParam.includes(tag)
)
if (tags.length > 0 || components.length > 0) {
return sandbox
}
})
const [tags, components] = getTagsAndComponents(sandboxes)
return json(
{
sandboxes: filteredSandboxes,
tags,
components,
},
{
headers: {
'Cache-Control': 'public, max-age=60, s-max-age=60',
},
}
)
} catch (err) {
console.error(err)
return redirect('/500')
}
}
export const action: ActionFunction = async ({ request }) => {
const form = await request.formData()
const tags = form.get('tags') ?? ''
const components = form.get('components') ?? ''
if (tags !== '' || components !== '') {
return redirect(
`/examples?${tags !== '' ? `tags=${tags}` : ''}${
components !== '' ? `&components=${components}` : ''
}`
)
}
return redirect(`/examples`)
}
export const meta: MetaFunction = () => {
return [
{
title: 'Examples | React Spring',
},
{
name: 'description',
content: `The home of examples using react-spring to bring naturally fluid animations elevating UI & interactions`,
},
{ property: 'og:title', content: 'Examples | React Spring' },
{
name: 'og:description',
contnet:
'The home of examples using react-spring to bring naturally fluid animations elevating UI & interactions',
},
{
name: 'og:url',
content: 'https://www.react-spring.dev/examples',
},
{
name: 'twitter:url',
content: 'https://www.react-spring.dev/examples',
},
{
name: 'twitter:title',
content: 'Examples | React Spring',
},
{
name: 'twitter:description',
content:
'The home of examples using react-spring to bring naturally fluid animations elevating UI & interactions',
},
]
}
export interface Sandbox {
urlTitle: string
title: string
tags: string[]
screenshotUrl: string
description: string
id: string
}
export default function Examples() {
const { components, sandboxes, tags } = useLoaderData<{
components: { value: string; label: string }[]
sandboxes: Sandbox[]
tags: { value: string; label: string }[]
}>()
const [searchParams] = useSearchParams()
const defaultTags = searchParams.getAll('tags')
const defaultComponents = searchParams.getAll('components')
const [selectStates, setSelectState] = useState({
tags: defaultTags.map(tag => ({ value: tag, label: tag })),
components: defaultComponents.map(component => ({
value: component,
label: component,
})),
})
const formRef = useRef<HTMLFormElement>(null!)
const fetcher = useFetcher()
const { state } = useNavigation()
const handleSelectChange =
(name: 'tags' | 'components') =>
(newValue: MultiValue<{ value: string }>) => {
const data = {
...selectStates,
[name]: newValue,
}
setSelectState(data)
fetcher.submit(
{
tags: data.tags.map(val => val.value).join(','),
components: data.components.map(val => val.value).join(','),
},
{ method: 'post', action: '/examples' }
)
}
return (
<>
<Header />
<main className={main}>
<div className={flex}>
<div>
<Heading fontStyle="XL" className={xlHeading}>
Examples
</Heading>
<Copy className={copy}>
{`Got an example you want to see here & share with the community?`}{' '}
Check out{' '}
<Anchor href="https://github.com/pmndrs/react-spring/tree/main/demo/CONTRIBUTING.md">
this guide
</Anchor>
.
</Copy>
<Form className={exampleFilters} method="post" ref={formRef}>
<Heading
tag="h2"
fontStyle="XS"
style={{ display: 'inline-block' }}
>
{`Alternatively, check out examples by `}
</Heading>
<Select
placeholder="Tags"
options={tags}
onChange={handleSelectChange('tags')}
value={selectStates.tags}
/>
<Heading
tag="h2"
fontStyle="XS"
style={{ display: 'inline-block' }}
>
{` or `}
</Heading>
<Select
placeholder="Components"
options={components}
onChange={handleSelectChange('components')}
value={selectStates.components}
/>
{selectStates.tags.length > 0 ||
selectStates.components.length > 0 ? (
<Heading tag="h2" fontStyle="XS" className={h2}>
{'. Or maybe, you want to see them '}
<Anchor href="/examples">all?</Anchor>
</Heading>
) : null}
</Form>
</div>
<div>
<WidgetCarbon />
</div>
</div>
<ul
className={sandboxesList}
style={{
opacity: state === 'loading' ? 0.5 : 1,
}}
>
{sandboxes.map(props => (
<li key={props.urlTitle}>
<CardExample {...props} />
</li>
))}
</ul>
</main>
</>
)
}
|