Update ElementToggle to use CSS modules

This commit is contained in:
Justin Edmund 2023-07-03 19:04:04 -07:00
parent 1b4c7ea588
commit ad2b70e819
2 changed files with 57 additions and 17 deletions

View file

@ -1,4 +1,4 @@
.ToggleGroup { .group {
$height: 36px; $height: 36px;
background-color: var(--toggle-bg); background-color: var(--toggle-bg);
@ -15,7 +15,7 @@
height: auto; height: auto;
} }
.ToggleItem { .item {
background: var(--toggle-bg); background: var(--toggle-bg);
border: none; border: none;
border-radius: 18px; border-radius: 18px;

View file

@ -1,74 +1,114 @@
import React from 'react' import React, { useEffect, useState } from 'react'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import { useTranslation } from 'next-i18next' import { useTranslation } from 'next-i18next'
import classNames from 'classnames'
import * as ToggleGroup from '@radix-ui/react-toggle-group' import * as ToggleGroup from '@radix-ui/react-toggle-group'
import styles from './index.module.scss' import styles from './index.module.scss'
interface Props { interface Props {
currentElement: number currentElement: number
sendValue: (value: string) => void sendValue: (value: number) => void
} }
const ElementToggle = (props: Props) => { const ElementToggle = ({ currentElement, sendValue, ...props }: Props) => {
// Router and localization
const router = useRouter() const router = useRouter()
const { t } = useTranslation('common')
const locale = const locale =
router.locale && ['en', 'ja'].includes(router.locale) ? router.locale : 'en' router.locale && ['en', 'ja'].includes(router.locale) ? router.locale : 'en'
const { t } = useTranslation('common')
// State: Component
const [element, setElement] = useState(currentElement)
// Methods: Handlers
const handleElementChange = (value: string) => {
const newElement = parseInt(value)
setElement(newElement)
sendValue(newElement)
}
// Methods: Rendering
return ( return (
<ToggleGroup.Root <ToggleGroup.Root
className="ToggleGroup" className={styles.group}
type="single" type="single"
defaultValue={`${props.currentElement}`} value={`${element}`}
aria-label="Element" aria-label="Element"
onValueChange={props.sendValue} onValueChange={handleElementChange}
> >
<ToggleGroup.Item <ToggleGroup.Item
className={`ToggleItem ${locale}`} className={classNames({
[styles.item]: true,
[styles[`${locale}`]]: true,
})}
value="0" value="0"
aria-label="null" aria-label="null"
> >
{t('elements.null')} {t('elements.null')}
</ToggleGroup.Item> </ToggleGroup.Item>
<ToggleGroup.Item <ToggleGroup.Item
className={`ToggleItem wind ${locale}`} className={classNames({
[styles.item]: true,
[styles.wind]: true,
[styles[`${locale}`]]: true,
})}
value="1" value="1"
aria-label="wind" aria-label="wind"
> >
{t('elements.wind')} {t('elements.wind')}
</ToggleGroup.Item> </ToggleGroup.Item>
<ToggleGroup.Item <ToggleGroup.Item
className={`ToggleItem fire ${locale}`} className={classNames({
[styles.item]: true,
[styles.fire]: true,
[styles[`${locale}`]]: true,
})}
value="2" value="2"
aria-label="fire" aria-label="fire"
> >
{t('elements.fire')} {t('elements.fire')}
</ToggleGroup.Item> </ToggleGroup.Item>
<ToggleGroup.Item <ToggleGroup.Item
className={`ToggleItem water ${locale}`} className={classNames({
[styles.item]: true,
[styles.water]: true,
[styles[`${locale}`]]: true,
})}
value="3" value="3"
aria-label="water" aria-label="water"
> >
{t('elements.water')} {t('elements.water')}
</ToggleGroup.Item> </ToggleGroup.Item>
<ToggleGroup.Item <ToggleGroup.Item
className={`ToggleItem earth ${locale}`} className={classNames({
[styles.item]: true,
[styles.earth]: true,
[styles[`${locale}`]]: true,
})}
value="4" value="4"
aria-label="earth" aria-label="earth"
> >
{t('elements.earth')} {t('elements.earth')}
</ToggleGroup.Item> </ToggleGroup.Item>
<ToggleGroup.Item <ToggleGroup.Item
className={`ToggleItem dark ${locale}`} className={classNames({
[styles.item]: true,
[styles.dark]: true,
[styles[`${locale}`]]: true,
})}
value="5" value="5"
aria-label="dark" aria-label="dark"
> >
{t('elements.dark')} {t('elements.dark')}
</ToggleGroup.Item> </ToggleGroup.Item>
<ToggleGroup.Item <ToggleGroup.Item
className={`ToggleItem light ${locale}`} className={classNames({
[styles.item]: true,
[styles.light]: true,
[styles[`${locale}`]]: true,
})}
value="6" value="6"
aria-label="light" aria-label="light"
> >