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

[IOPLT-278] Makes HeaderSecondLevel handle no go back action button and modal representation with no extra space on top #169

Merged
merged 6 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 17 additions & 1 deletion example/src/navigation/navigator.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import { HeaderSecondLevel } from "@pagopa/io-app-design-system";
import { Accordion } from "../pages/Accordion";
import { DSAdvice } from "../pages/Advice";
import { DSAlert } from "../pages/Alert";
Expand Down Expand Up @@ -324,7 +325,22 @@ const AppNavigator = () => (
name={APP_ROUTES.SCREENS.FULL_SCREEN_MODAL.route}
component={ListItems}
options={{
headerShown: false
headerShown: true,
header: ({ navigation }) => (
<HeaderSecondLevel
title={APP_ROUTES.SCREENS.FULL_SCREEN_MODAL.title}
transparent
isModal
type="singleAction"
firstAction={{
icon: "closeMedium",
onPress: () => {
navigation.goBack();
},
accessibilityLabel: ""
}}
/>
)
}}
/>
</Stack.Group>
Expand Down
105 changes: 105 additions & 0 deletions example/src/pages/HeaderSecondLevelForModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import * as React from "react";
import { useState } from "react";
import { Alert, View, LayoutChangeEvent } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import Animated, {
useAnimatedScrollHandler,
useSharedValue
} from "react-native-reanimated";
import { useNavigation } from "@react-navigation/native";
import {
Body,
H3,
HeaderSecondLevel,
IOVisualCostants,
VSpacer
} from "@pagopa/io-app-design-system";

// This is defined as about the half of a default ListItem… component
const defaultTriggerOffsetValue: number = 32;

export const HeaderSecondLevelScreen = () => {
const [triggerOffsetValue, setTriggerOffsetValue] = useState(
defaultTriggerOffsetValue
);
const translationY = useSharedValue(0);

const insets = useSafeAreaInsets();
const navigation = useNavigation();

const getTitleHeight = (event: LayoutChangeEvent) => {
const { height } = event.nativeEvent.layout;
setTriggerOffsetValue(height);
};

const scrollHandler = useAnimatedScrollHandler(event => {
// eslint-disable-next-line functional/immutable-data
translationY.value = event.contentOffset.y;
});

React.useLayoutEffect(() => {
navigation.setOptions({
headerTransparent: true,
header: () => (
<HeaderSecondLevel
scrollValues={{
contentOffsetY: translationY,
triggerOffset: triggerOffsetValue
}}
title={"Questo è un titolo lungo, ma lungo lungo davvero, eh!"}
// goBack={() => navigation.goBack()}
// backAccessibilityLabel="Torna indietro"
transparent={true}
type="singleAction"
firstAction={{
icon: "help",
onPress: () => {
Alert.alert("Contextual Help");
},
accessibilityLabel: ""
}}
// secondAction={{
// icon: "add",
// onPress: () => {
// Alert.alert("add");
// },
// accessibilityLabel: ""
// }}
// thirdAction={{
// icon: "coggle",
// onPress: () => {
// Alert.alert("Settings");
// },
// accessibilityLabel: ""
// }}
/>
)
});
}, [navigation, translationY, triggerOffsetValue]);

return (
<Animated.ScrollView
contentContainerStyle={{
marginTop: insets.top + IOVisualCostants.headerHeight,
paddingBottom: insets.bottom,
paddingHorizontal: IOVisualCostants.appMarginDefault
}}
onScroll={scrollHandler}
scrollEventThrottle={8}
snapToOffsets={[0, triggerOffsetValue]}
snapToEnd={false}
decelerationRate="normal"
>
<View
onLayout={getTitleHeight}
// style={{ backgroundColor: IOColors["hanPurple-500"] }}
>
<H3>Questo è un titolo lungo, ma lungo lungo davvero, eh!</H3>
</View>
<VSpacer />
{[...Array(50)].map((_el, i) => (
<Body key={`body-${i}`}>Repeated text</Body>
))}
</Animated.ScrollView>
);
};
38 changes: 28 additions & 10 deletions src/components/layout/HeaderSecondLevel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,22 @@ type ScrollValues = {
triggerOffset: number;
};

type BackProps =
| {
goBack: () => void;
backAccessibilityLabel: string;
}
| {
goBack?: never;
backAccessibilityLabel?: never;
};

type CommonProps = WithTestID<{
scrollValues?: ScrollValues;
title: string;
goBack: () => void;
backAccessibilityLabel: string;
// Visual attributes
transparent?: boolean;
isModal?: boolean;
}>;

interface Base extends CommonProps {
Expand Down Expand Up @@ -63,7 +72,8 @@ interface ThreeActions extends CommonProps {
thirdAction: ActionProp;
}

export type HeaderSecondLevel = Base | OneAction | TwoActions | ThreeActions;
export type HeaderSecondLevel = BackProps &
(Base | OneAction | TwoActions | ThreeActions);

const HEADER_BG_COLOR: IOColors = "white";
const borderColorDisabled = hexToRgba(IOColors["grey-100"], 0);
Expand Down Expand Up @@ -106,6 +116,7 @@ export const HeaderSecondLevel = ({
title,
type,
transparent = false,
isModal = false,
testID,
firstAction,
secondAction,
Expand Down Expand Up @@ -155,14 +166,21 @@ export const HeaderSecondLevel = ({
>
<Animated.View
testID={testID}
style={[{ marginTop: insets.top }, styles.headerInner]}
style={[isModal ? {} : { marginTop: insets.top }, styles.headerInner]}
>
<IconButton
icon={Platform.OS === "ios" ? "backiOS" : "backAndroid"}
color="neutral"
onPress={goBack}
accessibilityLabel={backAccessibilityLabel}
/>
{goBack ? (
<IconButton
icon={Platform.select({
android: "backAndroid",
default: "backiOS"
})}
color="neutral"
onPress={goBack}
accessibilityLabel={backAccessibilityLabel}
/>
) : (
<HSpacer size={32} />
)}
<Animated.Text
numberOfLines={1}
style={[
Expand Down