Files
notesnook/apps/mobile/src/components/Header/HeaderTitle.js

72 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-12-01 22:52:01 +05:00
import React, { useEffect } from 'react';
2020-11-10 17:16:06 +05:00
import Animated from 'react-native-reanimated';
2020-12-01 22:52:01 +05:00
import { useTracked } from '../../provider';
import { DDS } from '../../services/DeviceDetection';
import { eSubscribeEvent, eUnSubscribeEvent } from '../../services/EventManager';
import { eScrollEvent } from '../../utils/Events';
2020-11-04 17:35:24 +05:00
import Heading from '../Typography/Heading';
2020-05-10 22:17:14 +05:00
2020-11-20 01:23:05 +05:00
const opacity = new Animated.Value(DDS.isLargeTablet() ? 1 : 0);
2020-11-10 17:16:06 +05:00
let scrollPostions = {};
2020-11-04 09:55:55 +05:00
export const HeaderTitle = () => {
2020-11-04 17:35:24 +05:00
const [state] = useTracked();
2020-09-27 10:15:19 +05:00
const {colors, headerTextState} = state;
2020-11-10 17:16:06 +05:00
const onScroll = async (y) => {
2020-11-20 01:23:05 +05:00
if (DDS.isLargeTablet()) return;
if (typeof y !== 'number') {
if (y.type === 'back') {
scrollPostions[y.name] = null;
return;
}
if (scrollPostions[y.name]) {
if (scrollPostions[y.name] > 200) {
opacity.setValue(1);
} else {
scrollPostions[y.name] = 0;
opacity.setValue(0);
}
} else {
scrollPostions[y.name] = 0;
opacity.setValue(0);
}
return;
}
2020-11-10 17:16:06 +05:00
if (y > 75) {
let yVal = y - 75;
o = yVal / 75;
opacity.setValue(o);
} else {
opacity.setValue(0);
}
scrollPostions[headerTextState.heading] = y;
2020-11-10 17:16:06 +05:00
};
useEffect(() => {
eSubscribeEvent(eScrollEvent, onScroll);
return () => {
eUnSubscribeEvent(eScrollEvent, onScroll);
};
}, [headerTextState.heading]);
2021-01-10 10:31:16 +05:00
2020-05-10 22:17:14 +05:00
return (
2020-11-10 17:16:06 +05:00
<Animated.View
style={{
2021-01-09 16:07:43 +05:00
opacity: DDS.isLargeTablet() ? 1 : opacity,
2020-11-10 17:16:06 +05:00
}}>
2020-11-04 17:35:24 +05:00
<Heading color={headerTextState.color}>
2020-11-20 01:23:05 +05:00
<Heading color={colors.accent}>
2020-09-09 22:09:57 +05:00
{headerTextState.heading.slice(0, 1) === '#' ? '#' : null}
2020-11-20 01:23:05 +05:00
</Heading>
2020-05-10 22:17:14 +05:00
2020-09-09 22:09:57 +05:00
{headerTextState.heading.slice(0, 1) === '#'
? headerTextState.heading.slice(1)
: headerTextState.heading}
2020-11-04 17:35:24 +05:00
</Heading>
2020-11-10 17:16:06 +05:00
</Animated.View>
2020-05-10 22:17:14 +05:00
);
};