Skip to content

Commit 59642b2

Browse files
committed
Use fancy menu for presets
1 parent 111855f commit 59642b2

File tree

7 files changed

+38
-41
lines changed

7 files changed

+38
-41
lines changed

src/app/components/FancyMenu.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import { useFocus } from '../hooks/index.js'
44

55
interface Props {
66
placeholder?: string
7+
relative?: boolean
8+
class?: string
79
getResults: (search: string, close: () => void) => ComponentChildren
810
children: ComponentChildren
911
}
10-
export function FancyMenu({ placeholder, getResults, children }: Props) {
12+
export function FancyMenu({ placeholder, relative, class: clazz, getResults, children }: Props) {
1113
const [active, setActive] = useFocus()
1214
const [search, setSearch] = useState('')
1315
const inputRef = useRef<HTMLInputElement>(null)
@@ -51,13 +53,13 @@ export function FancyMenu({ placeholder, getResults, children }: Props) {
5153
}
5254
}, [setActive, inputRef])
5355

54-
return <div class="px-1 relative">
56+
return <div class={`px-1 ${relative ? 'relative' : ''}`}>
5557
<div onClick={open}>
5658
{children}
5759
</div>
58-
<div class={`fancy-menu absolute flex flex-col gap-2 p-2 rounded-lg drop-shadow-xl ${active ? '' : 'hidden'}`} onKeyDown={handleKeyDown}>
60+
<div class={`fancy-menu absolute flex flex-col gap-2 p-2 rounded-lg drop-shadow-xl ${clazz} ${active ? '' : 'hidden'}`} onKeyDown={handleKeyDown}>
5961
<input ref={inputRef} type="text" class="py-1 px-2 w-full rounded" value={search} placeholder={placeholder} onInput={(e) => setSearch((e.target as HTMLInputElement).value)} onClick={(e) => e.stopPropagation()} />
60-
{active && <div ref={resultsRef} class="overflow-y-auto overscroll-none flex flex-col pr-2 h-96 max-h-max min-w-max">
62+
{active && <div ref={resultsRef} class="fancy-menu-results overflow-y-auto overscroll-none flex flex-col pr-2 h-96 max-h-max w-max max-w-full">
6163
{results}
6264
</div>}
6365
</div>

src/app/components/Header.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function GeneratorTitle({ title, gen }: GeneratorTitleProps) {
8282
return [<span class="note">{locale('generators.no_results')}</span>]
8383
}
8484
return results.map(g =>
85-
<Link class="gen-result flex items-center cursor-pointer no-underline rounded p-1" href={cleanUrl(g.url)} onClick={close}>
85+
<Link class="flex items-center cursor-pointer no-underline rounded p-1" href={cleanUrl(g.url)} onClick={close}>
8686
{locale(`generator.${g.id}`)}
8787
{Object.keys(Icons).includes(g.id) ? Icons[g.id as keyof typeof Icons] : undefined}
8888
<div class="m-auto"></div>

src/app/components/forms/SearchList.tsx

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/app/components/forms/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export * from './Checkbox.js'
22
export * from './Input.js'
3-
export * from './SearchList.js'

src/app/components/generator/SchemaGenerator.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import { checkVersion, fetchDependencyMcdoc, fetchPreset, fetchRegistries, getSn
1313
import { DEPENDENCY_URI } from '../../services/Spyglass.js'
1414
import { Store } from '../../Store.js'
1515
import { cleanUrl, genPath } from '../../Utils.js'
16-
import { Ad, Btn, BtnMenu, ErrorPanel, FileCreation, FileView, Footer, HasPreview, Octicon, PreviewPanel, ProjectPanel, SearchList, SourcePanel, TextInput, VersionSwitcher } from '../index.js'
16+
import { FancyMenu } from '../FancyMenu.jsx'
17+
import { Ad, Btn, BtnMenu, ErrorPanel, FileCreation, FileView, Footer, HasPreview, Octicon, PreviewPanel, ProjectPanel, SourcePanel, TextInput, VersionSwitcher } from '../index.js'
1718
import { getRootDefault } from './McdocHelpers.js'
1819

1920
export const SHARE_KEY = 'share'
@@ -199,6 +200,23 @@ export function SchemaGenerator({ gen, allowedVersions }: Props) {
199200
return entries.map(e => e.startsWith('minecraft:') ? e.slice(10) : e)
200201
}, [version, gen.id])
201202

203+
const getPresets = useCallback((search: string, close: () => void) => {
204+
if (presets === undefined) {
205+
return <span class="w-80 note">{locale('loading')}</span>
206+
}
207+
if (!presets || presets.length === 0) {
208+
return <span class="w-80 note">{locale('presets.no_results')}</span>
209+
}
210+
const terms = search.trim().split(' ')
211+
const results = presets?.filter(v => terms.every(t => v.includes(t))).slice(0, 100) ?? []
212+
if (results.length === 0) {
213+
return <span class="w-80 note">{locale('presets.no_results_for_query')}</span>
214+
}
215+
return results.map(r => <button class="w-80 flex items-center cursor-pointer no-underline rounded p-1" onClick={() => {selectPreset(r); close()}}>
216+
{r}
217+
</button>)
218+
}, [presets])
219+
202220
const selectPreset = (id: string) => {
203221
Analytics.loadPreset(gen.id, id)
204222
setSharedSnippetId(undefined, true)
@@ -372,9 +390,9 @@ export function SchemaGenerator({ gen, allowedVersions }: Props) {
372390
{Octicon.mortar_board}
373391
<span>{locale('wiki')}</span>
374392
</a>}
375-
<BtnMenu icon="archive" label={locale('presets')} relative={false}>
376-
<SearchList searchPlaceholder={locale('search')} noResults={locale('no_presets')} values={presets} onSelect={selectPreset}/>
377-
</BtnMenu>
393+
<FancyMenu placeholder={locale('search')} getResults={getPresets} relative={false} class="right-0 mt-2">
394+
<Btn icon="archive" label={locale('presets')} />
395+
</FancyMenu>
378396
<VersionSwitcher value={version} onChange={selectVersion} allowed={allowedVersions} />
379397
<BtnMenu icon="kebab_horizontal" tooltip={locale('more')}>
380398
<Btn icon="history" label={locale('reset_default')} onClick={reset} />

src/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@
184184
"move_down": "Move down",
185185
"move_up": "Move up",
186186
"no_file_chosen": "No file chosen",
187-
"no_presets": "No presets",
188187
"normalize": "Normalize",
189188
"not_found.description": "The page you were looking for does not exist.",
190189
"output_settings": "Output settings",
@@ -196,6 +195,8 @@
196195
"partner.ohthetreesyoullgrow": "Oh The Trees You'll Grow",
197196
"partner.sky_aesthetics": "Sky Aesthetics",
198197
"presets": "Presets",
198+
"presets.no_results": "No presets",
199+
"presets.no_results_for_query": "No presets for this query",
199200
"preview": "Visualize",
200201
"preview.auto_scroll": "Auto scroll",
201202
"preview.biome": "Biome",

src/styles/global.css

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ nav li .btn svg {
226226
}
227227

228228
.fancy-menu {
229+
max-width: calc(100vw - 32px);
229230
background-color: var(--background-2);
230231
color: var(--text-2);
231232
}
@@ -234,11 +235,11 @@ nav li .btn svg {
234235
background-color: var(--background-1);
235236
}
236237

237-
.gen-result {
238+
.fancy-menu-results > * {
238239
outline-offset: -2px;
239240
}
240241

241-
.gen-result svg {
242+
.fancy-menu-results > * svg {
242243
width: 16px;
243244
height: 16px;
244245
fill: var(--nav);
@@ -247,13 +248,13 @@ nav li .btn svg {
247248
transition: margin 0.2s;
248249
}
249250

250-
.gen-result:focus-visible,
251-
.gen-result:hover {
251+
.fancy-menu-results > *:focus-visible,
252+
.fancy-menu-results > *:hover {
252253
background-color: var(--background-3);
253254
}
254255

255-
.gen-result:focus-visible svg,
256-
.gen-result:hover svg {
256+
.fancy-menu-results > *:focus-visible svg,
257+
.fancy-menu-results > *:hover svg {
257258
margin-left: 14px;
258259
margin-right: 0px;
259260
}

0 commit comments

Comments
 (0)