This commit is contained in:
2026-05-25 17:08:18 +08:00
parent df501f6151
commit f1259b71b5
9178 changed files with 1626125 additions and 0 deletions
@@ -0,0 +1,8 @@
import * as React from 'react';
import type { TourProps } from '../interface';
import type { TourStepInfo } from '../TourStep';
export declare function useClosable(stepClosable: TourStepInfo['closable'], stepCloseIcon: TourStepInfo['closeIcon'], closable: TourProps['closable'], closeIcon: TourProps['closeIcon']): {
closeIcon?: React.ReactNode;
} & React.AriaAttributes & {
[key: `data-${string}`]: unknown;
} & Pick<React.HTMLAttributes<HTMLDivElement>, "role">;
@@ -0,0 +1,35 @@
import * as React from 'react';
function isConfigObj(closable) {
return closable !== null && typeof closable === 'object';
}
/**
* Convert `closable` to ClosableConfig.
* When `preset` is true, will auto fill ClosableConfig with default value.
*/
function getClosableConfig(closable, closeIcon, preset) {
if (closable === false || closeIcon === false && (!isConfigObj(closable) || !closable.closeIcon)) {
return null;
}
const mergedCloseIcon = typeof closeIcon !== 'boolean' ? closeIcon : undefined;
if (isConfigObj(closable)) {
return {
...closable,
closeIcon: closable.closeIcon ?? mergedCloseIcon
};
}
// When StepClosable no need auto fill, but RootClosable need this.
return preset || closable || closeIcon ? {
closeIcon: mergedCloseIcon
} : 'empty';
}
export function useClosable(stepClosable, stepCloseIcon, closable, closeIcon) {
return React.useMemo(() => {
const stepClosableConfig = getClosableConfig(stepClosable, stepCloseIcon, false);
const rootClosableConfig = getClosableConfig(closable, closeIcon, true);
if (stepClosableConfig !== 'empty') {
return stepClosableConfig;
}
return rootClosableConfig;
}, [closable, closeIcon, stepClosable, stepCloseIcon]);
}
@@ -0,0 +1,14 @@
/// <reference types="react" />
import type { TourStepInfo } from '..';
export interface Gap {
offset?: number | [number, number];
radius?: number;
}
export interface PosInfo {
left: number;
top: number;
height: number;
width: number;
radius: number;
}
export default function useTarget(target: TourStepInfo['target'], open: boolean, gap?: Gap, scrollIntoViewOptions?: boolean | ScrollIntoViewOptions, inlineMode?: boolean, placeholderRef?: React.RefObject<HTMLDivElement>): [PosInfo, HTMLElement];
+90
View File
@@ -0,0 +1,90 @@
import useEvent from "@rc-component/util/es/hooks/useEvent";
import useLayoutEffect from "@rc-component/util/es/hooks/useLayoutEffect";
import { useMemo, useState } from 'react';
import { isInViewPort } from "../util";
function isValidNumber(val) {
return typeof val === 'number' && !Number.isNaN(val);
}
export default function useTarget(target, open, gap, scrollIntoViewOptions, inlineMode, placeholderRef) {
// ========================= Target =========================
// We trade `undefined` as not get target by function yet.
// `null` as empty target.
const [targetElement, setTargetElement] = useState(undefined);
useLayoutEffect(() => {
const nextElement = typeof target === 'function' ? target() : target;
setTargetElement(nextElement || null);
});
// ========================= Align ==========================
const [posInfo, setPosInfo] = useState(null);
const updatePos = useEvent(() => {
if (targetElement) {
// Exist target element. We should scroll and get target position
if (!inlineMode && !isInViewPort(targetElement) && open) {
targetElement.scrollIntoView(scrollIntoViewOptions);
}
const {
left,
top,
width,
height
} = targetElement.getBoundingClientRect();
const nextPosInfo = {
left,
top,
width,
height,
radius: 0
};
// If `inlineMode` we need cut off parent offset
if (inlineMode) {
const parentRect = placeholderRef.current?.parentElement?.getBoundingClientRect();
if (parentRect) {
nextPosInfo.left -= parentRect.left;
nextPosInfo.top -= parentRect.top;
}
}
setPosInfo(origin => {
if (JSON.stringify(origin) !== JSON.stringify(nextPosInfo)) {
return nextPosInfo;
}
return origin;
});
} else {
// Not exist target which means we just show in center
setPosInfo(null);
}
});
const getGapOffset = index => (Array.isArray(gap?.offset) ? gap?.offset[index] : gap?.offset) ?? 6;
useLayoutEffect(() => {
updatePos();
// update when window resize
window.addEventListener('resize', updatePos);
// update when `document.body.style.overflow` is set to visible
// by default, it will be set to hidden
window.addEventListener('scroll', updatePos);
return () => {
window.removeEventListener('resize', updatePos);
window.removeEventListener('scroll', updatePos);
};
}, [targetElement, open, updatePos]);
// ======================== PosInfo =========================
const mergedPosInfo = useMemo(() => {
if (!posInfo) {
return posInfo;
}
const gapOffsetX = getGapOffset(0);
const gapOffsetY = getGapOffset(1);
const gapRadius = isValidNumber(gap?.radius) ? gap?.radius : 2;
return {
left: posInfo.left - gapOffsetX,
top: posInfo.top - gapOffsetY,
width: posInfo.width + gapOffsetX * 2,
height: posInfo.height + gapOffsetY * 2,
radius: gapRadius
};
}, [posInfo, gap]);
return [mergedPosInfo, targetElement];
}