2
0
Files
bot/apps/builder/components/theme/GeneralSettings/FontSelector/FontSelector.tsx

48 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-12-23 09:37:42 +01:00
import React, { useEffect, useState } from 'react'
import { Text, HStack } from '@chakra-ui/react'
import { SearchableDropdown } from '../../../shared/SearchableDropdown'
2021-12-23 09:37:42 +01:00
type FontSelectorProps = {
activeFont?: string
onSelectFont: (font: string) => void
}
export const FontSelector = ({
activeFont,
onSelectFont,
}: FontSelectorProps) => {
const [currentFont, setCurrentFont] = useState(activeFont)
const [googleFonts, setGoogleFonts] = useState<string[]>([])
useEffect(() => {
fetchPopularFonts().then(setGoogleFonts)
}, [])
const fetchPopularFonts = async () => {
if (!process.env.NEXT_PUBLIC_GOOGLE_API_KEY) return []
2021-12-23 09:37:42 +01:00
const response = await fetch(
2022-03-17 17:02:30 +01:00
`https://www.googleapis.com/webfonts/v1/webfonts?key=${process.env.NEXT_PUBLIC_GOOGLE_API_KEY}&sort=popularity`
2021-12-23 09:37:42 +01:00
)
return (await response.json()).items.map(
(item: { family: string }) => item.family
)
}
2022-01-22 18:24:57 +01:00
const handleFontSelected = (nextFont: string) => {
if (nextFont == currentFont) return
setCurrentFont(nextFont)
onSelectFont(nextFont)
}
2021-12-23 09:37:42 +01:00
return (
<HStack justify="space-between" align="center">
2021-12-23 09:37:42 +01:00
<Text>Font</Text>
<SearchableDropdown
selectedItem={activeFont}
items={googleFonts}
2022-01-22 18:24:57 +01:00
onValueChange={handleFontSelected}
2021-12-23 09:37:42 +01:00
/>
</HStack>
2021-12-23 09:37:42 +01:00
)
}