TypeScript
All flag components are fully typed with TypeScript, providing autocomplete and type safety out of the box.
SVG prop typing
Section titled “SVG prop typing”All flag components accept standard React SVG props:
import type { SVGProps } from 'react'import { FlagUs } from '@sankyu/react-circle-flags'
interface Props { flagProps?: SVGProps<SVGSVGElement>}
export function FlagItem({ flagProps }: Props) { return <FlagUs {...flagProps} />}All flag components accept standard Vue SVG props:
<script setup lang="ts">import type { SVGAttributes } from 'vue'import { FlagUs } from '@sankyu/vue-circle-flags'
interface Props { flagProps?: SVGAttributes}
const props = defineProps<Props>()</script>
<template> <FlagUs v-bind="props.flagProps" /></template>All flag components accept standard Solid SVG props:
import type { JSX } from 'solid-js'import { FlagUs } from '@sankyu/solid-circle-flags'
interface Props { flagProps?: JSX.SvgSVGAttributes<SVGSVGElement>}
export function FlagItem(props: Props) { return <FlagUs {...props.flagProps} />}Advanced typing examples
Section titled “Advanced typing examples”import type { SVGProps, ComponentProps } from 'react'import { FlagCn } from '@sankyu/react-circle-flags'
// Method 1: Using SVGPropstype FlagProps1 = SVGProps<SVGSVGElement>
// Method 2: Using ComponentProps (recommended)type FlagProps2 = ComponentProps<typeof FlagCn>
export function Flag(props: FlagProps2) { return <FlagCn {...props} />}import type { ComponentProps, ComponentType } from 'react'import { FlagUs } from '@sankyu/react-circle-flags'
type SvgFlagProps = ComponentProps<typeof FlagUs>type SvgFlagComponent = ComponentType<SvgFlagProps>
interface FlagWithLabelProps extends SvgFlagProps { FlagComponent?: SvgFlagComponent label?: string}
export function FlagWithLabel({ FlagComponent = FlagUs, label, ...svgProps}: FlagWithLabelProps) { return ( <div> <FlagComponent {...svgProps} /> {label && <span>{label}</span>} </div> )}import type { MouseEvent } from 'react'import { FlagJp } from '@sankyu/react-circle-flags'
export function InteractiveFlag() { const handleClick = (e: MouseEvent<SVGSVGElement>) => { console.log('Flag clicked at', e.clientX, e.clientY) }
return ( <FlagJp width={48} height={48} onClick={handleClick} style={{ cursor: 'pointer' }} /> )}import type { Component } from 'solid-js'import { FlagCn, FlagUs } from '@sankyu/solid-circle-flags'
type FlagCnProps = Parameters<typeof FlagCn>[0]type SvgFlagProps = Parameters<typeof FlagUs>[0]type SvgFlagComponent = Component<SvgFlagProps>
export function Flag(props: FlagCnProps) { return <FlagCn {...props} />}
export function FlagWithLabel( props: { FlagComponent?: SvgFlagComponent; label?: string } & SvgFlagProps) { const { FlagComponent = FlagUs, label, ...svgProps } = props return ( <div> <FlagComponent {...svgProps} /> {label && <span>{label}</span>} </div> )}Type exports
Section titled “Type exports”The library exports helpful types for advanced use cases:
import { coerceFlagCode, isFlagCode } from '@sankyu/react-circle-flags'import type { CountryCode, FlagCode, FlagComponentProps } from '@sankyu/react-circle-flags'
// FlagCode: Union type of all available flag codesconst validCode: FlagCode = 'us' // ✓ Validconst invalidCode: FlagCode = 'invalid' // ✗ Type error
// CountryCode: a semantic alias of FlagCode (same union)const alsoValid: CountryCode = validCode
// FlagComponentProps: Standard props for flag componentsconst props: FlagComponentProps = { width: 64, height: 64, className: 'my-flag',}
// Runtime data narrowing/coercion helpersconst fromApi: string = 'US'const safeCode = coerceFlagCode(fromApi) // -> FlagCode (unknown values become "xx")const isNormalizedValid = isFlagCode('us') // true (expects already-normalized input)import { coerceFlagCode, isFlagCode } from '@sankyu/vue-circle-flags'import type { CountryCode, FlagCode, FlagComponentProps } from '@sankyu/vue-circle-flags'
// FlagCode: Union type of all available flag codesconst validCode: FlagCode = 'us' // ✓ Validconst invalidCode: FlagCode = 'invalid' // ✗ Type error
// CountryCode: a semantic alias of FlagCode (same union)const alsoValid: CountryCode = validCode
// FlagComponentProps: Standard props for flag componentsconst props: FlagComponentProps = { width: 64, height: 64, class: 'my-flag',}
const fromApi: string = 'US'const safeCode = coerceFlagCode(fromApi)const isNormalizedValid = isFlagCode('us')import { coerceFlagCode, isFlagCode } from '@sankyu/solid-circle-flags'import type { CountryCode, FlagCode, FlagComponentProps } from '@sankyu/solid-circle-flags'
// FlagCode: Union type of all available flag codesconst validCode: FlagCode = 'us' // ✓ Validconst invalidCode: FlagCode = 'invalid' // ✗ Type error
// CountryCode: a semantic alias of FlagCode (same union)const alsoValid: CountryCode = validCode
// FlagComponentProps: Standard props for flag componentsconst props: FlagComponentProps = { width: 64, height: 64, className: 'my-flag',}
const fromApi: string = 'US'const safeCode = coerceFlagCode(fromApi)const isNormalizedValid = isFlagCode('us')Flag code utilities
Section titled “Flag code utilities”import { FlagUtils } from '@sankyu/react-circle-flags'
// Convert country code to component name (PascalCase)FlagUtils.getComponentName('us') // 'FlagUs'FlagUtils.getComponentName('gb-eng') // 'FlagGbEng'
// Normalize display format (uppercase)FlagUtils.formatCountryCode('us') // 'US'import { FlagUtils } from '@sankyu/vue-circle-flags'
FlagUtils.getComponentName('us') // 'FlagUs'FlagUtils.formatCountryCode('us') // 'US'import { FlagUtils } from '@sankyu/solid-circle-flags'
FlagUtils.getComponentName('us') // 'FlagUs'FlagUtils.formatCountryCode('us') // 'US'FlagUtils toolkit
Section titled “FlagUtils toolkit”The FlagUtils object provides convenient utility functions for working with flag codes:
import { FlagUtils } from '@sankyu/react-circle-flags'
// Validate country codesFlagUtils.isValidCountryCode('us') // trueFlagUtils.isValidCountryCode('invalid') // false
// Format country codesFlagUtils.formatCountryCode('us') // "US"
// Get component namesFlagUtils.getComponentName('us') // "FlagUs"
// Handle flag sizesFlagUtils.getSizeName(48) // "lg"FlagUtils.sizes.lg // 48import { FlagUtils } from '@sankyu/vue-circle-flags'
FlagUtils.isValidCountryCode('us') // trueFlagUtils.formatCountryCode('us') // "US"FlagUtils.getComponentName('us') // "FlagUs"FlagUtils.getSizeName(48) // "lg"FlagUtils.sizes.lg // 48import { FlagUtils } from '@sankyu/solid-circle-flags'
FlagUtils.isValidCountryCode('us') // trueFlagUtils.formatCountryCode('us') // "US"FlagUtils.getComponentName('us') // "FlagUs"FlagUtils.getSizeName(48) // "lg"FlagUtils.sizes.lg // 48Type safety with DynamicFlag
Section titled “Type safety with DynamicFlag”import { DynamicFlag } from '@sankyu/react-circle-flags'import type { FlagCode } from '@sankyu/react-circle-flags'
interface CountrySelectProps { selectedCountry: FlagCode onSelect: (code: FlagCode) => void}
export function CountrySelect({ selectedCountry, onSelect }: CountrySelectProps) { return ( <div> <DynamicFlag strict code={selectedCountry} width={32} height={32} /> <button onClick={() => onSelect('us')}>USA</button> <button onClick={() => onSelect('cn')}>China</button> </div> )}<script setup lang="ts">import { ref } from 'vue'import { DynamicFlag } from '@sankyu/vue-circle-flags'import type { FlagCode } from '@sankyu/vue-circle-flags'
const selectedCountry = ref<FlagCode>('us')</script>
<template> <DynamicFlag strict :code="selectedCountry" :width="32" :height="32" /> <button @click="selectedCountry = 'us'">USA</button> <button @click="selectedCountry = 'cn'">China</button></template>import { createSignal } from 'solid-js'import { DynamicFlag } from '@sankyu/solid-circle-flags'import type { FlagCode } from '@sankyu/solid-circle-flags'
export function CountrySelect() { const [selectedCountry, setSelectedCountry] = createSignal<FlagCode>('us')
return ( <div> <DynamicFlag strict code={selectedCountry()} width={32} height={32} /> <button onClick={() => setSelectedCountry('us')}>USA</button> <button onClick={() => setSelectedCountry('cn')}>China</button> </div> )}Next step
Section titled “Next step” Bundle Size & Tree-shaking Learn how to optimize your bundle size with proper import strategies