Skip to content

Commit 58df1a6

Browse files
Copilotmaotoumao
andcommitted
Refactor: Extract background insets logic into reusable hook
Created useBackgroundInsets hook to reduce code duplication and improve maintainability. This hook encapsulates the logic for calculating negative safe area insets used to extend backgrounds beyond safe area boundaries. Co-authored-by: maotoumao <31655147+maotoumao@users.noreply.github.com>
1 parent a0ae2ee commit 58df1a6

3 files changed

Lines changed: 24 additions & 24 deletions

File tree

src/components/base/pageBackground.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { StyleSheet, View } from "react-native";
33
import Image from "./image";
44
import useColors from "@/hooks/useColors";
55
import Theme from "@/core/theme";
6-
import { useSafeAreaInsets } from "react-native-safe-area-context";
6+
import useBackgroundInsets from "@/hooks/useBackgroundInsets";
77

88
function PageBackground() {
99
const theme = Theme.useTheme();
1010
const background = Theme.useBackground();
1111
const colors = useColors();
12-
const insets = useSafeAreaInsets();
12+
const backgroundInsets = useBackgroundInsets();
1313

1414
return (
1515
<>
@@ -19,10 +19,7 @@ function PageBackground() {
1919
{
2020
backgroundColor:
2121
colors?.pageBackground ?? colors.background,
22-
top: -insets.top,
23-
bottom: -insets.bottom,
24-
left: -insets.left,
25-
right: -insets.right,
22+
...backgroundInsets,
2623
},
2724
]}
2825
/>
@@ -33,10 +30,7 @@ function PageBackground() {
3330
style.wrapper,
3431
{
3532
opacity: background?.opacity ?? 0.6,
36-
top: -insets.top,
37-
bottom: -insets.bottom,
38-
left: -insets.left,
39-
right: -insets.right,
33+
...backgroundInsets,
4034
},
4135
]}
4236
blurRadius={background?.blur ?? 20}

src/hooks/useBackgroundInsets.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { useSafeAreaInsets } from "react-native-safe-area-context";
2+
3+
/**
4+
* Hook that returns negative safe area insets for extending backgrounds
5+
* beyond safe area boundaries to cover the entire screen.
6+
*/
7+
export default function useBackgroundInsets() {
8+
const insets = useSafeAreaInsets();
9+
10+
return {
11+
top: -insets.top,
12+
bottom: -insets.bottom,
13+
left: -insets.left,
14+
right: -insets.right,
15+
};
16+
}

src/pages/musicDetail/components/background.tsx

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import React, { useMemo } from "react";
22
import { Image, StyleSheet, View } from "react-native";
33
import { ImgAsset } from "@/constants/assetsConst";
44
import { useCurrentMusic } from "@/core/trackPlayer";
5-
import { useSafeAreaInsets } from "react-native-safe-area-context";
5+
import useBackgroundInsets from "@/hooks/useBackgroundInsets";
66

77
export default function Background() {
88
const musicItem = useCurrentMusic();
9-
const insets = useSafeAreaInsets();
9+
const backgroundInsets = useBackgroundInsets();
1010

1111
const artworkSource = useMemo(() => {
1212
if (!musicItem?.artwork) {
@@ -26,22 +26,12 @@ export default function Background() {
2626
<>
2727
<View style={[
2828
style.background,
29-
{
30-
top: -insets.top,
31-
bottom: -insets.bottom,
32-
left: -insets.left,
33-
right: -insets.right,
34-
}
29+
backgroundInsets
3530
]} />
3631
<Image
3732
style={[
3833
style.blur,
39-
{
40-
top: -insets.top,
41-
bottom: -insets.bottom,
42-
left: -insets.left,
43-
right: -insets.right,
44-
}
34+
backgroundInsets
4535
]}
4636
blurRadius={50}
4737
source={artworkSource}

0 commit comments

Comments
 (0)