♻ Rework the search bar

This commit is contained in:
Aj - Thomas
2022-05-16 13:50:08 +02:00
committed by ajnart
parent e786b1e44f
commit 0d11244506

View File

@@ -1,6 +1,6 @@
import { TextInput, Text, Popover, Box } from '@mantine/core'; import { TextInput, Text, Popover, Kbd, Group } from '@mantine/core';
import { useForm } from '@mantine/hooks'; import { useForm, useHotkeys } from '@mantine/hooks';
import { useState } from 'react'; import { useRef, useState } from 'react';
import { Search, BrandYoutube, Download } from 'tabler-icons-react'; import { Search, BrandYoutube, Download } from 'tabler-icons-react';
import { useConfig } from '../../tools/state'; import { useConfig } from '../../tools/state';
@@ -9,6 +9,16 @@ export default function SearchBar(props: any) {
const [opened, setOpened] = useState(false); const [opened, setOpened] = useState(false);
const [icon, setIcon] = useState(<Search />); const [icon, setIcon] = useState(<Search />);
const queryUrl = config.settings.searchUrl || 'https://www.google.com/search?q='; const queryUrl = config.settings.searchUrl || 'https://www.google.com/search?q=';
const textInput: any = useRef(null);
useHotkeys([['ctrl+K', () => textInput.current.focus()]]);
const rightSection = (
<div style={{ display: 'flex', alignItems: 'center' }}>
<Kbd>Ctrl</Kbd>
<span style={{ margin: '0 5px' }}>+</span>
<Kbd>K</Kbd>
</div>
);
const form = useForm({ const form = useForm({
initialValues: { initialValues: {
@@ -21,12 +31,6 @@ export default function SearchBar(props: any) {
} }
return ( return (
<Box
mb="xl"
style={{
width: '100%',
}}
>
<form <form
onChange={() => { onChange={() => {
// If query contains !yt or !t add "Searching on YouTube" or "Searching torrent" // If query contains !yt or !t add "Searching on YouTube" or "Searching torrent"
@@ -57,35 +61,38 @@ export default function SearchBar(props: any) {
> >
<Popover <Popover
opened={opened} opened={opened}
style={{
width: '100%',
}}
position="bottom" position="bottom"
placement="start" placement="start"
width={260}
withArrow withArrow
radius="md"
trapFocus={false} trapFocus={false}
transition="pop-top-left" transition="pop-bottom-right"
onFocusCapture={() => setOpened(true)} onFocusCapture={() => setOpened(true)}
onBlurCapture={() => setOpened(false)} onBlurCapture={() => setOpened(false)}
target={ target={
<Group direction="row">
<TextInput <TextInput
variant="filled" variant="filled"
color="blue"
icon={icon} icon={icon}
ref={textInput}
rightSectionWidth={90}
rightSection={rightSection}
radius="md" radius="md"
size="md" size="md"
placeholder="Search the web" styles={{ rightSection: { pointerEvents: 'none' } }}
placeholder="Search the web..."
{...props} {...props}
{...form.getInputProps('query')} {...form.getInputProps('query')}
/> />
</Group>
} }
> >
<Text> <Text>
tip: Use the prefixes <b>!yt</b> and <b>!t</b> in front of your query to search on tip: Use the prefixes <b>!yt</b> and <b>!t</b> in front of your query to search on YouTube
YouTube or for a Torrent respectively. or for a Torrent respectively.
</Text> </Text>
</Popover> </Popover>
</form> </form>
</Box>
); );
} }