Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(Cross): [IOAPPX-307] Add useScreenEndMargin hook #5808

Merged
merged 18 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
5c0d4b8
Add the new `useScreenEndMargin` hook
dmnplb May 31, 2024
f216798
Add the new `DSScreeEndMargin`
dmnplb May 31, 2024
c088132
Apply `screenEndMargin` to the DS screens
dmnplb Jun 3, 2024
adf85c2
Refactor `ZendeskSupportHelpCenter` to use the new hook
dmnplb Jun 3, 2024
2db498e
Merge branch 'master' into IOAPPX-307-add-use-screen-end-margin-hook
dmnplb Jun 3, 2024
d85d261
Merge branch 'master' into IOAPPX-307-add-use-screen-end-margin-hook
dmnplb Jun 3, 2024
53f09b0
Merge branch 'master' into IOAPPX-307-add-use-screen-end-margin-hook
dmnplb Jun 3, 2024
17bb09b
Merge branch 'master' into IOAPPX-307-add-use-screen-end-margin-hook
dmnplb Jun 4, 2024
7418a81
Merge branch 'master' into IOAPPX-307-add-use-screen-end-margin-hook
dmnplb Jun 4, 2024
1a78063
Merge branch 'master' into IOAPPX-307-add-use-screen-end-margin-hook
dmnplb Jun 5, 2024
d25cbb0
Merge branch 'master' into IOAPPX-307-add-use-screen-end-margin-hook
dmnplb Jun 6, 2024
9f6532b
Merge branch 'master' into IOAPPX-307-add-use-screen-end-margin-hook
dmnplb Jun 6, 2024
1805be8
Remove useless `useMemo` from `useScreenEndMargin`
dmnplb Jun 6, 2024
b0ef3cf
Refactor `FooterActions`, remove useless `useMemo`
dmnplb Jun 6, 2024
8500df0
Remove some useless `useMemo` from `IOScrollView` too
dmnplb Jun 6, 2024
e019a4c
Merge branch 'master' into IOAPPX-307-add-use-screen-end-margin-hook
dmnplb Jun 6, 2024
d2d4794
Remove any trace of `useMemo` from `IOScrollView`
dmnplb Jun 6, 2024
d9e3f56
Remove useless types and `useMemo`
dmnplb Jun 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 75 additions & 79 deletions ts/components/ui/FooterActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ import {
useIOTheme
} from "@pagopa/io-app-design-system";
import * as React from "react";
import {
ComponentProps,
Fragment,
PropsWithChildren,
useMemo,
useState
} from "react";
import { ComponentProps, Fragment, PropsWithChildren, useState } from "react";
import {
ColorValue,
LayoutChangeEvent,
Expand Down Expand Up @@ -143,10 +137,10 @@ export const FooterActions = ({
const theme = useIOTheme();
const { isExperimental } = useIOExperimentalDesign();

const type = actions?.type;
const primaryAction = actions?.primary;
const secondaryAction = actions?.secondary;
const tertiaryAction = actions?.tertiary;
const { bottomMargin, extraBottomMargin } = useBottomMargins(
actions,
excludeSafeAreaMargins
);

/* Total height of actions */
const [actionBlockHeight, setActionBlockHeight] =
Expand All @@ -158,41 +152,15 @@ export const FooterActions = ({
const TRANSPARENT_BG_COLOR: ColorValue = "transparent";
const BUTTONSOLID_HEIGHT = isExperimental ? buttonSolidHeight : 40;

const insets = useSafeAreaInsets();
const needSafeAreaMargin = useMemo(() => insets.bottom !== 0, [insets]);
const safeAreaMargin = useMemo(() => insets.bottom, [insets]);

/* Check if the iPhone bottom handle is present.
If not, or if you don't need safe area insets,
add a default margin to prevent the button
from sticking to the bottom. */
const bottomMargin: number = useMemo(
() =>
!needSafeAreaMargin || excludeSafeAreaMargins
? IOVisualCostants.appMarginDefault
: safeAreaMargin,
[needSafeAreaMargin, excludeSafeAreaMargins, safeAreaMargin]
);

/* When the secondary action is visible, add extra margin
to avoid little space from iPhone bottom handle */
const extraBottomMargin: number = useMemo(
() => (secondaryAction && needSafeAreaMargin ? extraSafeAreaMargin : 0),
[needSafeAreaMargin, secondaryAction]
);

/* Safe background block. Cover everything until it reaches
the half of the primary action button. It avoids
glitchy behavior underneath. */
const safeBackgroundBlockHeight: number = useMemo(
() => bottomMargin + actionBlockHeight - BUTTONSOLID_HEIGHT / 2,
[BUTTONSOLID_HEIGHT, actionBlockHeight, bottomMargin]
);
const safeBackgroundBlockHeight =
bottomMargin + actionBlockHeight - BUTTONSOLID_HEIGHT / 2;

const getActionBlockMeasurements = (event: LayoutChangeEvent) => {
const { height } = event.nativeEvent.layout;
setActionBlockHeight(height);

/* Height of the safe bottom area, applied to the ScrollView:
Actions + Content end margin */
const safeBottomAreaHeight = bottomMargin + height + contentEndMargin;
Expand Down Expand Up @@ -247,48 +215,76 @@ export const FooterActions = ({
<Text style={styles.debugText}>{`Height: ${actionBlockHeight}`}</Text>
)}

{primaryAction && <ButtonSolid fullWidth {...primaryAction} />}
{renderActions(actions, extraBottomMargin)}
</View>
</Animated.View>
);
};

const useBottomMargins = (
actions: FooterActions | undefined,
excludeSafeAreaMargins: boolean
) => {
const insets = useSafeAreaInsets();
const needSafeAreaMargin = insets.bottom !== 0;

{type === "TwoButtons" && (
<View
style={{
alignSelf: "center",
marginBottom: extraBottomMargin
}}
>
<VSpacer size={spaceBetweenActionAndLink} />
{secondaryAction && (
<ButtonLink
color="primary"
{...(secondaryAction as ComponentProps<typeof ButtonLink>)}
/>
)}
</View>
)}
/* Check if the iPhone bottom handle is present.
If not, or if you don't need safe area insets,
add a default margin to prevent the button
from sticking to the bottom. */
const bottomMargin =
!needSafeAreaMargin || excludeSafeAreaMargins
? IOVisualCostants.appMarginDefault
: insets.bottom;

{type === "ThreeButtons" && (
<Fragment>
{secondaryAction && (
<Fragment>
<VSpacer size={spaceBetweenActions} />
<ButtonOutline fullWidth color="primary" {...secondaryAction} />
</Fragment>
)}
/* When the secondary action is visible, add extra margin
to avoid little space from iPhone bottom handle */
const extraBottomMargin =
actions?.secondary && needSafeAreaMargin ? extraSafeAreaMargin : 0;

{tertiaryAction && (
<View
style={{
alignSelf: "center",
marginBottom: extraBottomMargin
}}
>
<VSpacer size={spaceBetweenActionAndLink} />
<ButtonLink color="primary" {...tertiaryAction} />
</View>
)}
</Fragment>
)}
</View>
</Animated.View>
return { bottomMargin, extraBottomMargin };
};

const renderActions = (
actions: FooterActions | undefined,
extraBottomMargin: number
) => {
if (!actions) {
return null;
}
const {
type,
primary: primaryAction,
secondary: secondaryAction,
tertiary: tertiaryAction
} = actions;
return (
<Fragment>
{primaryAction && <ButtonSolid fullWidth {...primaryAction} />}
{type === "TwoButtons" && secondaryAction && (
<View style={{ alignSelf: "center", marginBottom: extraBottomMargin }}>
<VSpacer size={spaceBetweenActionAndLink} />
<ButtonLink color="primary" {...secondaryAction} />
</View>
)}
{type === "ThreeButtons" && (
<>
{secondaryAction && (
<>
<VSpacer size={spaceBetweenActions} />
<ButtonOutline fullWidth color="primary" {...secondaryAction} />
</>
)}
{tertiaryAction && (
<View
style={{ alignSelf: "center", marginBottom: extraBottomMargin }}
>
<VSpacer size={spaceBetweenActionAndLink} />
<ButtonLink color="primary" {...tertiaryAction} />
</View>
)}
</>
)}
</Fragment>
);
};
Loading
Loading