Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: hydration error

Blue Mouse172a9f1dc6bb
diff --git a/web/app/components/pricing/BillingPricing.tsx b/web/app/components/pricing/BillingPricing.tsx
index a4ac54b37..060274ed6 100644
--- a/web/app/components/pricing/BillingPricing.tsx
+++ b/web/app/components/pricing/BillingPricing.tsx
@@ -308,6 +308,14 @@ const BillingPricing = ({
return _round(discountPercentage, 0)
}, [tiers, currencyCode])
+ const toggleBillingFrequency = () => {
+ setBillingFrequency((currentFrequency) =>
+ currentFrequency === BillingFrequency.yearly
+ ? BillingFrequency.monthly
+ : BillingFrequency.yearly,
+ )
+ }
+
return (
<>
<div className='rounded-xl border border-gray-200 p-4 sm:p-6 dark:border-white/10'>
@@ -317,13 +325,7 @@ const BillingPricing = ({
</h2>
<button
type='button'
- onClick={() =>
- setBillingFrequency(
- billingFrequency === BillingFrequency.yearly
- ? BillingFrequency.monthly
- : BillingFrequency.yearly,
- )
- }
+ onClick={toggleBillingFrequency}
className='flex cursor-pointer items-center gap-2 rounded-lg border border-gray-200 bg-gray-100 px-3 py-2 transition-colors hover:bg-gray-200 dark:border-white/10 dark:bg-slate-950 dark:hover:bg-slate-900'
>
<span className='text-sm font-medium text-gray-700 dark:text-gray-200'>
@@ -336,13 +338,7 @@ const BillingPricing = ({
) : null}
<Switch
checked={billingFrequency === BillingFrequency.yearly}
- onChange={() =>
- setBillingFrequency(
- billingFrequency === BillingFrequency.yearly
- ? BillingFrequency.monthly
- : BillingFrequency.yearly,
- )
- }
+ visualOnly
/>
</button>
</div>
diff --git a/web/app/components/pricing/MarketingPricing.tsx b/web/app/components/pricing/MarketingPricing.tsx
index ef70bf890..1cb9aec5d 100644
--- a/web/app/components/pricing/MarketingPricing.tsx
+++ b/web/app/components/pricing/MarketingPricing.tsx
@@ -56,6 +56,14 @@ const MarketingPricing = ({
return _round(discountPercentage, 0)
}, [plans, currencyCode])
+ const toggleBillingFrequency = () => {
+ setBillingFrequency((currentFrequency) =>
+ currentFrequency === BillingFrequency.yearly
+ ? BillingFrequency.monthly
+ : BillingFrequency.yearly,
+ )
+ }
+
return (
<section id='pricing' className='relative p-2'>
<div className='rounded-xl bg-slate-900 py-16 sm:py-20 dark:bg-slate-900/50'>
@@ -152,7 +160,7 @@ const MarketingPricing = ({
? t('main.goToBilling')
: t('main.startAXDayFreeTrial', { amount: TRIAL_DAYS })}
</span>
- <ArrowRightIcon className='mt-[1px] h-4 w-5' />
+ <ArrowRightIcon className='mt-px h-4 w-5' />
</Link>
</div>
@@ -160,13 +168,7 @@ const MarketingPricing = ({
<div className='mb-3 flex justify-end'>
<button
type='button'
- onClick={() =>
- setBillingFrequency(
- billingFrequency === BillingFrequency.yearly
- ? BillingFrequency.monthly
- : BillingFrequency.yearly,
- )
- }
+ onClick={toggleBillingFrequency}
className='flex cursor-pointer items-center gap-2 rounded-lg border border-white/10 bg-slate-900 px-3 py-2 transition-colors hover:bg-slate-800'
>
<span className='text-sm font-medium text-gray-100'>
@@ -179,13 +181,7 @@ const MarketingPricing = ({
) : null}
<Switch
checked={billingFrequency === BillingFrequency.yearly}
- onChange={() =>
- setBillingFrequency(
- billingFrequency === BillingFrequency.yearly
- ? BillingFrequency.monthly
- : BillingFrequency.yearly,
- )
- }
+ visualOnly
/>
</button>
</div>
diff --git a/web/app/ui/Switch.tsx b/web/app/ui/Switch.tsx
index d1d7e11cc..6fbf1689f 100644
--- a/web/app/ui/Switch.tsx
+++ b/web/app/ui/Switch.tsx
@@ -1,15 +1,139 @@
import cx from 'clsx'
-import { useId } from 'react'
+import { type MouseEvent, type ReactNode, useId } from 'react'
import { Text } from '~/ui/Text'
interface SwitchProps {
checked: boolean
- onChange: (checked: boolean) => void
+ onChange?: (checked: boolean) => void
disabled?: boolean
className?: string
label?: string
id?: string
+ visualOnly?: boolean
+}
+
+interface SwitchContainerProps {
+ className?: string
+ children: ReactNode
+}
+
+interface SwitchTrackProps {
+ checked: boolean
+ disabled: boolean
+ interactive?: boolean
+ id?: string
+ onClick?: (event: MouseEvent<HTMLButtonElement>) => void
+}
+
+interface SwitchLabelProps {
+ label?: string
+ disabled: boolean
+ interactive: boolean
+ htmlFor?: string
+}
+
+const SwitchContainer = ({ className, children }: SwitchContainerProps) => {
+ return (
+ <div className={cx('inline-flex items-center', className)}>{children}</div>
+ )
+}
+
+const SwitchTrack = ({
+ checked,
+ disabled,
+ interactive = false,
+ id,
+ onClick,
+}: SwitchTrackProps) => {
+ const thumb = (
+ <span
+ aria-hidden='true'
+ className={cx(
+ 'pointer-events-none inline-block size-3 transform rounded-full shadow-lg ring-0 transition duration-200 ease-in-out',
+ {
+ 'translate-x-3': checked,
+ 'translate-x-0': !checked,
+ 'bg-white dark:bg-slate-950': checked,
+ 'bg-white': !checked,
+ },
+ )}
+ />
+ )
+
+ const baseClassName =
+ 'relative inline-flex h-4 w-7 shrink-0 rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out'
+
+ const stateClassName = cx({
+ 'bg-slate-900 dark:bg-slate-200': checked,
+ 'bg-gray-300 dark:bg-slate-600': !checked,
+ 'cursor-not-allowed opacity-50': disabled,
+ })
+
+ if (!interactive) {
+ return (
+ <span aria-hidden='true' className={cx(baseClassName, stateClassName)}>
+ {thumb}
+ </span>
+ )
+ }
+
+ return (
+ <button
+ id={id}
+ type='button'
+ role='switch'
+ aria-checked={checked}
+ disabled={disabled}
+ onClick={onClick}
+ className={cx(
+ baseClassName,
+ 'cursor-pointer focus:outline-none focus-visible:ring-2 focus-visible:ring-slate-900 focus-visible:ring-offset-2 dark:focus-visible:ring-slate-300 dark:focus-visible:ring-offset-slate-900',
+ stateClassName,
+ )}
+ >
+ {thumb}
+ </button>
+ )
+}
+
+const SwitchLabel = ({
+ label,
+ disabled,
+ interactive,
+ htmlFor,
+}: SwitchLabelProps) => {
+ if (!label) return null
+
+ if (interactive) {
+ return (
+ <Text
+ as='label'
+ size='sm'
+ colour='primary'
+ htmlFor={htmlFor}
+ className={cx('ml-2', {
+ 'cursor-pointer': !disabled,
+ 'cursor-not-allowed opacity-50': disabled,
+ })}
+ >
+ {label}
+ </Text>
+ )
+ }
+
+ return (
+ <Text
+ as='span'
+ size='sm'
+ colour='primary'
+ className={cx('ml-2', {
+ 'opacity-50': disabled,
+ })}
+ >
+ {label}
+ </Text>
+ )
}
export const Switch = ({
@@ -19,61 +143,41 @@ export const Switch = ({
className,
label,
id,
+ visualOnly = false,
}: SwitchProps) => {
const generatedId = useId()
const switchId = id || generatedId
+ if (visualOnly) {
+ return (
+ <SwitchContainer className={className}>
+ <SwitchTrack checked={checked} disabled={disabled} />
+ <SwitchLabel label={label} disabled={disabled} interactive={false} />
+ </SwitchContainer>
+ )
+ }
+
return (
- <div className={cx('inline-flex items-center', className)}>
- <button
- id={switchId}
- type='button'
- role='switch'
- aria-checked={checked}
+ <SwitchContainer className={className}>
+ <SwitchTrack
+ checked={checked}
disabled={disabled}
- onClick={(e) => {
- e.preventDefault()
- e.stopPropagation()
- if (!disabled) {
+ interactive
+ id={switchId}
+ onClick={(event) => {
+ event.preventDefault()
+ event.stopPropagation()
+ if (!disabled && onChange) {
onChange(!checked)
}
}}
- className={cx(
- 'relative inline-flex h-4 w-7 shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus-visible:ring-2 focus-visible:ring-slate-900 focus-visible:ring-offset-2 dark:focus-visible:ring-slate-300 dark:focus-visible:ring-offset-slate-900',
- {
- 'bg-slate-900 dark:bg-slate-200': checked,
- 'bg-gray-300 dark:bg-slate-600': !checked,
- 'cursor-not-allowed opacity-50': disabled,
- },
- )}
- >
- <span
- aria-hidden='true'
- className={cx(
- 'pointer-events-none inline-block size-3 transform rounded-full shadow-lg ring-0 transition duration-200 ease-in-out',
- {
- 'translate-x-3': checked,
- 'translate-x-0': !checked,
- 'bg-white dark:bg-slate-950': checked,
- 'bg-white': !checked,
- },
- )}
- />
- </button>
- {label ? (
- <Text
- as='label'
- size='sm'
- colour='primary'
- htmlFor={switchId}
- className={cx('ml-2', {
- 'cursor-pointer': !disabled,
- 'cursor-not-allowed opacity-50': disabled,
- })}
- >
- {label}
- </Text>
- ) : null}
- </div>
+ />
+ <SwitchLabel
+ label={label}
+ disabled={disabled}
+ interactive
+ htmlFor={switchId}
+ />
+ </SwitchContainer>
)
}