import { useState, useEffect } from '@wordpress/element'; import { Flex, FlexBlock, RangeControl, TextControl, Button, Notice, } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { parseRangeToArray } from './utils'; type Props = { fontItem: GoogleFontItem; range: string; isWeightOneValue: boolean; widthData?: { range: string, default: string }; isWidthOneValue?: boolean; onChange: (newValues: GoogleFontItem) => void; }; // 注意:fontStyle, fontWidthは未実装で進める. export default function GoogleVariableFontSettings({ fontItem, range, widthData, isWeightOneValue, isWidthOneValue, onChange }: Props) { const [weight, setWeight] = useState(fontItem.variants[0]); const [isWeightCustom, setIsWeightCustom] = useState(isWeightOneValue); const allWeights = parseRangeToArray(range); // 親から渡されたpropsの変更に応じて状態を更新 useEffect(() => { // setFontStyle(fontItem.italic); setWeight(fontItem.variants[0]); setIsWeightCustom(isWeightOneValue); // setFontWidth(fontItem.width); }, [fontItem]); // onChangeのロジック useEffect(() => { onChange({ ...fontItem, variants: [weight], }); }, [weight, isWeightCustom]); return (
{/* Weight options */} {__('Weight', 'apura')}
{/* Weight selector */} {isWeightCustom ? ( setWeight(value?.toString() || '400')} allowReset initialPosition={400} min={Number(allWeights[0]) || 100} max={Number(allWeights[allWeights.length - 1]) || 900} step={1} /> ) : ( {}} /> )}
); }