Skip to content

Commit 410213c

Browse files
dieguezguillecruzdanilo
authored andcommitted
✨ app: create shared switch component
1 parent 648d55b commit 410213c

4 files changed

Lines changed: 62 additions & 38 deletions

File tree

.changeset/spotty-rules-camp.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@exactly/mobile": patch
3+
---
4+
5+
✨ create shared switch component

src/components/card/Card.tsx

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useRouter } from "expo-router";
66

77
import { ChevronRight, CircleHelp, CreditCard, DollarSign, Eye, EyeOff, Hash, Snowflake } from "@tamagui/lucide-icons";
88
import { useToastController } from "@tamagui/toast";
9-
import { ScrollView, Separator, Spinner, Square, Switch, XStack, YStack } from "tamagui";
9+
import { ScrollView, Separator, Spinner, Square, XStack, YStack } from "tamagui";
1010

1111
import { useMutation, useQuery } from "@tanstack/react-query";
1212

@@ -44,6 +44,7 @@ import LatestActivity from "../shared/LatestActivity";
4444
import PluginUpgrade from "../shared/PluginUpgrade";
4545
import SafeView from "../shared/SafeView";
4646
import Skeleton from "../shared/Skeleton";
47+
import Switch from "../shared/Switch";
4748
import Text from "../shared/Text";
4849
import View from "../shared/View";
4950

@@ -347,6 +348,10 @@ export default function Card() {
347348
{cardDetails && (
348349
<>
349350
<XStack
351+
role="switch"
352+
aria-checked={displayStatus === "FROZEN"}
353+
aria-label={t("Freeze card")}
354+
aria-disabled={isFetchingCard || isSettingCardStatus}
350355
justifyContent="space-between"
351356
paddingVertical="$s4"
352357
alignItems="center"
@@ -372,27 +377,9 @@ export default function Card() {
372377
{t("Freeze card")}
373378
</Text>
374379
</XStack>
375-
<XStack alignItems="center" justifyContent="center" height={24}>
376-
<Switch
377-
scale={0.9}
378-
margin={0}
379-
padding="$s1"
380-
pointerEvents="none"
381-
checked={displayStatus === "FROZEN"}
382-
backgroundColor={
383-
displayStatus === "FROZEN" ? "$backgroundBrandMild" : "$backgroundStrong"
384-
}
385-
borderWidth={0}
386-
height={24}
387-
width={60}
388-
>
389-
<Switch.Thumb
390-
checked={displayStatus === "FROZEN"}
391-
animation="default"
392-
backgroundColor={displayStatus === "FROZEN" ? "$backgroundBrand" : "$backgroundSoft"}
393-
/>
394-
</Switch>
395-
</XStack>
380+
<Switch checked={displayStatus === "FROZEN"}>
381+
<Switch.Thumb />
382+
</Switch>
396383
</XStack>
397384
<Separator borderColor="$borderNeutralSoft" />
398385
</>

src/components/home/CardStatus.tsx

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { selectionAsync } from "expo-haptics";
1616

1717
import { CalendarDays, ChevronRight, CreditCard, Info, Snowflake, Wallet, Zap } from "@tamagui/lucide-icons";
1818
import { useToastController } from "@tamagui/toast";
19-
import { AnimatePresence, Spinner, Square, Switch, useTheme, View, XStack, YStack } from "tamagui";
19+
import { AnimatePresence, Spinner, Square, useTheme, View, XStack, YStack } from "tamagui";
2020

2121
import { useMutation, useQuery } from "@tanstack/react-query";
2222

@@ -25,6 +25,7 @@ import Exa from "../../assets/images/exa.svg";
2525
import queryClient from "../../utils/queryClient";
2626
import reportError from "../../utils/reportError";
2727
import { setCardStatus, type CardDetails } from "../../utils/server";
28+
import Switch from "../shared/Switch";
2829
import Text from "../shared/Text";
2930

3031
export default function CardStatus({
@@ -210,6 +211,10 @@ export default function CardStatus({
210211
exitStyle={{ opacity: 0, transform: [{ translateY: -8 }] }}
211212
>
212213
<XStack
214+
role="switch"
215+
aria-checked={frozen}
216+
aria-label={t("Freeze card")}
217+
aria-disabled={isSettingCardStatus}
213218
justifyContent="space-between"
214219
paddingVertical="$s4"
215220
alignItems="center"
@@ -232,21 +237,9 @@ export default function CardStatus({
232237
{t("Freeze card")}
233238
</Text>
234239
</XStack>
235-
<XStack alignItems="center" justifyContent="center" height={24}>
236-
<Switch
237-
scale={0.9}
238-
margin={0}
239-
padding="$s1"
240-
pointerEvents="none"
241-
checked={frozen}
242-
backgroundColor="$backgroundBrandMild"
243-
borderWidth={0}
244-
height={24}
245-
width={60}
246-
>
247-
<Switch.Thumb checked={frozen} animation="default" backgroundColor="$backgroundBrand" />
248-
</Switch>
249-
</XStack>
240+
<Switch checked={frozen}>
241+
<Switch.Thumb />
242+
</Switch>
250243
</XStack>
251244
</YStack>
252245
)}

src/components/shared/Switch.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { createStyledContext, styled, View, withStaticProperties } from "tamagui";
2+
3+
const SwitchContext = createStyledContext<{ checked: boolean }>({ checked: false });
4+
5+
const SwitchFrame = styled(View, {
6+
name: "Switch",
7+
context: SwitchContext,
8+
"aria-hidden": true,
9+
width: "$s8",
10+
height: "$s5",
11+
borderRadius: "$r_0",
12+
padding: "$s1",
13+
animation: "default",
14+
animateOnly: ["backgroundColor"],
15+
variants: {
16+
checked: {
17+
true: { backgroundColor: "$backgroundBrandMild" },
18+
false: { backgroundColor: "$backgroundStrong" },
19+
},
20+
} as const,
21+
});
22+
23+
const SwitchThumb = styled(View, {
24+
name: "SwitchThumb",
25+
context: SwitchContext,
26+
width: "$s4_5",
27+
height: "$s4_5",
28+
borderRadius: "$r_0",
29+
animation: "default",
30+
animateOnly: ["transform", "backgroundColor"],
31+
variants: {
32+
checked: {
33+
true: { backgroundColor: "$backgroundBrand", x: "$s5" },
34+
false: { backgroundColor: "$backgroundSoft", x: 0 },
35+
},
36+
} as const,
37+
});
38+
39+
export default withStaticProperties(SwitchFrame, { Thumb: SwitchThumb });

0 commit comments

Comments
 (0)