File size: 8,231 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
import { useRef } from 'react';
import { shouldUseStepContainerV2 } from 'calypso/landing/stepper/declarative-flow/helpers/should-use-step-container-v2';
import { DEFAULT_FLOW, getFlowFromURL } from 'calypso/landing/stepper/utils/get-flow-from-url';
import { isWpMobileApp } from 'calypso/lib/mobile-app';
let lastScrollPosition = 0; // Used for calculating scroll direction.
let sidebarTop = 0; // Current sidebar top position.
let pinnedSidebarTop = true; // We pin sidebar to the top by default.
let pinnedSidebarBottom = false;
let ticking = false; // Used for Scroll event throttling.
export function shouldLoadInlineHelp( sectionName: string, currentRoute: string ) {
if ( isWpMobileApp() ) {
return false;
}
const exemptedSections = [ 'jetpack-connect', 'devdocs', 'help', 'home' ];
const exemptedRoutes = [ '/log-in/jetpack' ];
const exemptedRoutesStartingWith = [
'/start/p2',
'/start/setup-site',
'/start/newsletter',
'/plugins/domain',
'/plugins/marketplace/setup',
];
return (
! exemptedSections.includes( sectionName ) &&
! exemptedRoutes.includes( currentRoute ) &&
! exemptedRoutesStartingWith.some( ( startsWithString ) =>
currentRoute.startsWith( startsWithString )
)
);
}
export const handleScroll = ( event: React.UIEvent< HTMLElement > ): void => {
// Do not run until next requestAnimationFrame or if running out of browser context.
if ( ticking ) {
return;
}
const windowHeight = window.innerHeight;
const contentEl = document.getElementById( 'content' );
const contentHeight = contentEl?.scrollHeight;
const secondaryEl = document.getElementById( 'secondary' ); // Or referred as sidebar.
const secondaryElHeight = secondaryEl?.scrollHeight;
const masterbarHeight = document.getElementById( 'header' )?.getBoundingClientRect().height;
// Check whether we need to adjust content height so that scroll events are triggered.
// Sidebar has overflow: initial and position:fixed, so content is our only chance for scroll events.
if ( contentEl && contentHeight && masterbarHeight && secondaryElHeight ) {
if ( contentHeight < secondaryElHeight ) {
// Adjust the content height so that it matches the sidebar + masterbar vertical scroll estate.
contentEl.style.minHeight = secondaryElHeight + masterbarHeight + 'px';
}
if (
windowHeight >= secondaryElHeight + masterbarHeight &&
contentEl &&
secondaryEl &&
( contentEl.style.minHeight !== 'initial' || secondaryEl.style.position )
) {
// In case that window is taller than the sidebar we reinstate the content min-height. CSS code: client/layout/style.scss:30.
contentEl.style.minHeight = 'initial';
// In case that window is taller than the sidebar after resize we need to clean up any previously set inline styles
secondaryEl.removeAttribute( 'style' );
}
}
if (
secondaryEl &&
secondaryElHeight !== undefined &&
masterbarHeight !== undefined &&
( secondaryElHeight + masterbarHeight > windowHeight || 'resize' === event.type ) // Only run when sidebar & masterbar are taller than window height OR we have a resize event
) {
// Throttle scroll event
window.requestAnimationFrame( function () {
const maxScroll = secondaryElHeight + masterbarHeight - windowHeight; // Max sidebar inner scroll.
const scrollY = -document.body.getBoundingClientRect().top; // Get current scroll position.
// Check for overscrolling, this happens when swiping up at the top of the document in modern browsers.
if ( scrollY < 0 ) {
// Stick the sidebar to the top.
if ( ! pinnedSidebarTop ) {
pinnedSidebarTop = true;
pinnedSidebarBottom = false;
secondaryEl.style.position = 'fixed';
secondaryEl.style.top = '0';
secondaryEl.style.bottom = '0';
}
ticking = false;
return;
} else if ( scrollY + windowHeight > document.body.scrollHeight - 1 ) {
// When overscrolling at the bottom, stick the sidebar to the bottom.
if ( ! pinnedSidebarBottom ) {
pinnedSidebarBottom = true;
pinnedSidebarTop = false;
secondaryEl.style.position = 'fixed';
secondaryEl.style.top = 'inherit';
secondaryEl.style.bottom = '0';
}
ticking = false;
return;
}
if ( scrollY >= lastScrollPosition ) {
// When a down scroll has been detected.
if ( pinnedSidebarTop ) {
pinnedSidebarTop = false;
sidebarTop = masterbarHeight;
if ( scrollY > maxScroll ) {
//In case we have already passed the available scroll of the sidebar, add the current scroll
sidebarTop += scrollY;
}
secondaryEl.style.position = 'absolute';
secondaryEl.style.top = `${ sidebarTop }px`;
secondaryEl.style.bottom = 'inherit';
} else if (
! pinnedSidebarBottom &&
scrollY + masterbarHeight > maxScroll + secondaryEl.offsetTop
) {
// Pin it to the bottom.
pinnedSidebarBottom = true;
secondaryEl.style.position = 'fixed';
secondaryEl.style.top = 'inherit';
secondaryEl.style.bottom = '0';
}
} else if ( scrollY < lastScrollPosition ) {
// When a scroll up is detected.
// If it was pinned to the bottom, unpin and calculate relative scroll.
if ( pinnedSidebarBottom ) {
pinnedSidebarBottom = false;
// Calculate new offset position.
sidebarTop = Math.max( 0, scrollY + masterbarHeight - maxScroll );
if ( contentHeight === secondaryElHeight + masterbarHeight ) {
// When content is originally shorter than the sidebar and
// we have already made it equal to the sidebar + masterbar
// the offset will always be the masterbar height (top position).
sidebarTop = masterbarHeight;
}
secondaryEl.style.position = 'absolute';
secondaryEl.style.top = `${ sidebarTop }px`;
secondaryEl.style.bottom = 'inherit';
} else if ( ! pinnedSidebarTop && scrollY + masterbarHeight < sidebarTop ) {
// Pin it to the top.
pinnedSidebarTop = true;
sidebarTop = masterbarHeight;
secondaryEl.style.position = 'fixed';
secondaryEl.style.top = `${ sidebarTop }px`;
secondaryEl.style.bottom = 'inherit';
}
}
lastScrollPosition = scrollY;
ticking = false;
} );
ticking = true;
}
};
const isRedirectingToStepContainerV2Flow = ( redirectTo: string ) => {
const { pathname, search } = new URL( redirectTo, 'http://example.com' );
return shouldUseStepContainerV2( getFlowFromURL( pathname, search ) );
};
const isMarketplaceThankYouRedirect = ( redirectTo: string ) => {
const { pathname, searchParams } = new URL( redirectTo, 'http://example.com' );
return pathname.startsWith( '/marketplace' ) && searchParams.has( 'onboarding' );
};
/**
* Returns whether to display the StepContainerV2 features from up in the tree.
* This can be used, for example, to determine if we should show
* the StepContainerV2 loader or hide the masterbar.
*/
export const isInStepContainerV2FlowContext = ( pathname: string, query: string ) => {
if ( pathname.startsWith( '/setup' ) ) {
return shouldUseStepContainerV2( getFlowFromURL( pathname, query ) || DEFAULT_FLOW );
}
if ( pathname.startsWith( '/checkout' ) ) {
// The checkout isn't technically part of a stepper flow, but we can infer what stepper
// flow it came from (if any) by inspecting the redirect_to query param (in the case
// of the onboarding flow).
const params = new URLSearchParams( query );
const redirectTo = params.get( 'redirect_to' ) ?? '';
const cancelTo = params.get( 'cancel_to' ) ?? '';
if ( isRedirectingToStepContainerV2Flow( redirectTo ) ) {
return true;
}
if ( isRedirectingToStepContainerV2Flow( cancelTo ) ) {
return true;
}
if ( isMarketplaceThankYouRedirect( redirectTo ) ) {
return true;
}
}
if ( pathname.startsWith( '/marketplace' ) ) {
const params = new URLSearchParams( query );
if ( params.has( 'onboarding' ) ) {
return true;
}
const redirectTo = params.get( 'redirect_to' ) ?? '';
return isMarketplaceThankYouRedirect( redirectTo );
}
return false;
};
export const useInitialIsInStepContainerV2FlowContext = () => {
const ref = useRef(
isInStepContainerV2FlowContext( window.location.pathname, window.location.search )
);
return ref.current;
};
|