import { FuzzySearch } from '@o/fuzzy-search'
import { IconNamesList } from '@o/icons'
import { isDefined, mergeDefined } from '@o/utils'
import React, { CSSProperties, memo, Suspense, useCallback, useContext } from 'react'
import { Config } from './helpers/configureUI'
import { IconPropsContext } from './IconPropsContext'
import { useScale } from './Scale'
import { Tooltip } from './Tooltip'
import { ViewProps } from './View/types'
import { View } from './View/View'
export type IconProps = ViewProps & {
size?: number
name?: string
tooltip?: string
tooltipProps?: Object
svg?: string
ignoreColor?: boolean
}
const names = IconNamesList
const searcher = new FuzzySearch(names.map(name => ({ name })), ['name'])
const nameCache = {}
export const findIconName = (name: string): string => {
if (!name || typeof name !== 'string') {
console.warn(`Bad name provided`, name)
name = ''
}
if (nameCache[name]) return nameCache[name]
let match = ''
const index = names.indexOf(name)
if (index > -1) {
match = names[index]
} else {
const result = searcher.search(name)
match = result.length ? result[0].name : 'dot'
}
nameCache[name] = match
return match
}
// lets users wrap around icons
export const Icon = memo((rawProps: IconProps) => {
const extraProps = useContext(IconPropsContext)
const props = extraProps ? mergeDefined(extraProps, rawProps) : rawProps
const ResolvedIcon = Config.useIcon || PlainIcon
return (
}>
)
})
// @ts-ignore
Icon.acceptsProps = {
icon: true,
hover: true,
}
const SIZE_STANDARD = 16
const SIZE_LARGE = 20
export const PlainIcon = (iconProps: Omit & { style: CSSProperties }) => {
let { style, ignoreColor, svg, tooltip, tooltipProps, name, opacity, ...props } = iconProps
const size = snapToSizes(props.size) * useScale()
if (typeof name === 'string') {
const nameTrim = name.trim()
if (nameTrim.indexOf('