Skip to content

Commit

Permalink
feat(navbar): animate navbar for large and medium size
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed Sep 8, 2022
1 parent 9bc4ef8 commit 30adb31
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
18 changes: 15 additions & 3 deletions src/react/components/Navbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const Navbar = forwardRef((props, ref) => {
const innerElRef = useRef(null);
const titleContainerElRef = useRef(null);
const titleElRef = useRef(null);
const subnavbarElRef = useRef(null);

const wasScrollable = useRef(null);

Expand Down Expand Up @@ -99,7 +100,9 @@ const Navbar = forwardRef((props, ref) => {
const maxTranslate = titleContainerHeight.current;
const scrollProgress = Math.max(Math.min(scrollTop / maxTranslate, 1), 0);

bgElRef.current.style.opacity = transparent ? scrollProgress : '';
bgElRef.current.style.opacity = transparent
? -0.5 + scrollProgress * 1.5
: '';
if (medium || large) {
bgElRef.current.style.transform = `translateY(-${
scrollProgress * maxTranslate
Expand All @@ -113,7 +116,12 @@ const Navbar = forwardRef((props, ref) => {
titleContainerElRef.current.style.opacity = 1 - scrollProgress * 2;
}
if (titleElRef.current) {
titleElRef.current.style.opacity = -0.5 + scrollProgress * 2;
titleElRef.current.style.opacity = -0.5 + scrollProgress * 1.5;
}
if ((medium || large) && subnavbarElRef.current) {
subnavbarElRef.current.style.transform = `translateY(-${
scrollProgress * maxTranslate
}px)`;
}
};

Expand Down Expand Up @@ -210,7 +218,11 @@ const Navbar = forwardRef((props, ref) => {
{title}
</div>
)}
{subnavbar && <div className={c.subnavbar}>{subnavbar}</div>}
{subnavbar && (
<div className={c.subnavbar} ref={subnavbarElRef}>
{subnavbar}
</div>
)}
</Component>
);
});
Expand Down
12 changes: 9 additions & 3 deletions src/shared/classes/NavbarClasses.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const NavbarClasses = (props, colors, classes) => {
base: {
common: cls(
`w-full z-20 top-0 pt-safe`,
(large || medium) && 'pointer-events-none',
positionClass('sticky', classes)
),
ios: cls(fontSizeIos, colors.textIos),
Expand All @@ -55,15 +56,17 @@ export const NavbarClasses = (props, colors, classes) => {
subnavbar: {
common: cls(
'relative flex items-center',
subnavbarClassName || subnavbarClass
subnavbarClassName || subnavbarClass,
(large || medium) && 'pointer-events-auto'
),
ios: 'h-11 pl-2-safe pr-2-safe',
material: 'h-14 pl-4-safe pr-4-safe',
},
inner: {
common: cls(
'flex relative items-center w-full overflow-hidden',
innerClassName || innerClass
innerClassName || innerClass,
(large || medium) && 'pointer-events-auto z-10'
),
ios: cls(
'pl-2-safe pr-2-safe h-11',
Expand All @@ -72,7 +75,10 @@ export const NavbarClasses = (props, colors, classes) => {
material: 'justify-start h-16 pl-safe pr-safe',
},
titleContainer: {
common: 'flex items-center px-4 relative',
common: cls(
'flex items-center px-4 relative',
(large || medium) && 'pointer-events-auto'
),
ios: cls(
medium && cls(titleMediumFontSizeIos, 'h-11 font-semibold'),
large && cls(titleLargeFontSizeIos, 'h-13 font-bold')
Expand Down
18 changes: 12 additions & 6 deletions src/svelte/components/Navbar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,20 @@
let innerElRef = null;
let titleContainerElRef = null;
let titleElRef = null;
let subnavbarElRef = null;
$: isScrollable = medium || large || transparent;
let wasScrollable = isScrollable;
const dark = useDarkClasses();
let theme;
theme = useTheme((v) => (theme = v));
$: colors = NavbarColors(colorsProp, dark);
$: isOutline = typeof outline === 'undefined' ? theme === 'ios' : outline;
let theme;
theme = useTheme((v) => (theme = v));
$: c = useThemeClasses(
{ ios, material },
NavbarClasses(
Expand Down Expand Up @@ -128,7 +129,7 @@
const maxTranslate = titleContainerHeight;
const scrollProgress = Math.max(Math.min(scrollTop / maxTranslate, 1), 0);
bgElRef.style.opacity = transparent ? scrollProgress : '';
bgElRef.style.opacity = transparent ? -0.5 + scrollProgress * 1.5 : '';
if (medium || large) {
bgElRef.style.transform = `translateY(-${
scrollProgress * maxTranslate
Expand All @@ -142,7 +143,12 @@
titleContainerElRef.style.opacity = 1 - scrollProgress * 2;
}
if (titleElRef) {
titleElRef.style.opacity = -0.5 + scrollProgress * 2;
titleElRef.style.opacity = -0.5 + scrollProgress * 1.5;
}
if ((medium || large) && subnavbarElRef) {
subnavbarElRef.style.transform = `translateY(-${
scrollProgress * maxTranslate
}px)`;
}
};
Expand Down Expand Up @@ -230,7 +236,7 @@
</div>
{/if}
{#if $$slots.subnavbar}
<div class={c.subnavbar}>
<div class={c.subnavbar} bind:this={subnavbarElRef}>
<slot name="subnavbar" />
</div>
{/if}
Expand Down
15 changes: 12 additions & 3 deletions src/vue/components/Navbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
{{ title }}
<slot name="title" />
</div>
<div v-if="slots.subnavbar" :class="c.subnavbar">
<div v-if="slots.subnavbar" ref="subnavbarElRef" :class="c.subnavbar">
<slot name="subnavbar" />
</div>
</component>
Expand Down Expand Up @@ -98,6 +98,7 @@
const innerElRef = ref(null);
const titleContainerElRef = ref(null);
const titleElRef = ref(null);
const subnavbarElRef = ref(null);
const isScrollable = computed(
() => props.transparent || props.large || props.medium
Expand Down Expand Up @@ -164,7 +165,9 @@
0
);
bgElRef.value.style.opacity = props.transparent ? scrollProgress : '';
bgElRef.value.style.opacity = props.transparent
? -0.5 + scrollProgress * 1.5
: '';
if (props.medium || props.large) {
bgElRef.value.style.transform = `translateY(-${
scrollProgress * maxTranslate
Expand All @@ -178,7 +181,12 @@
titleContainerElRef.value.style.opacity = 1 - scrollProgress * 2;
}
if (titleElRef.value) {
titleElRef.value.style.opacity = -0.5 + scrollProgress * 2;
titleElRef.value.style.opacity = -0.5 + scrollProgress * 1.5;
}
if ((props.medium || props.large) && subnavbarElRef.value) {
subnavbarElRef.value.style.transform = `translateY(-${
scrollProgress * maxTranslate
}px)`;
}
};
Expand Down Expand Up @@ -249,6 +257,7 @@
innerElRef,
titleContainerElRef,
titleElRef,
subnavbarElRef,
};
},
};
Expand Down

0 comments on commit 30adb31

Please sign in to comment.