Skip to content
Open
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
8 changes: 5 additions & 3 deletions src/calc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export interface IStickyParameters<S = any> {
nextElement(): IElementParameters | null;
}

export type IZIndexCalculation = (sticky: boolean, styles: ICssStyleData, layout: IProcessedStickyLayout) => number;

export type IStickyBehavior<S = any> = (
params: IStickyParameters<S>
) => IStickyLayout;
Expand Down Expand Up @@ -61,7 +63,7 @@ export interface IStickyHandle {
labels: ILabels | undefined;
selectorFunction: ISelectorFunction | undefined;
placeholderRef: RefObject<HTMLElement | undefined>;
update(stickyCopy: boolean, stickyCopyCss: ICssStyleData): void;
update(stickyCopy: boolean, stickyCopyCss: ICssStyleData, layout: IProcessedStickyLayout): void;
}

function memoize<T>(f: () => T): () => T {
Expand Down Expand Up @@ -244,7 +246,7 @@ export function updateStickyLayout(
placeholderOffset,
placeholderWidth
);
stickyHandleElement.data.update(sticky, cssProps);
stickyHandleElement.data.update(sticky, cssProps, layout);
}
});

Expand Down Expand Up @@ -287,7 +289,7 @@ function cssifyStickyLayout(
const sticky = true;
const { top, z, fixedHeight } = layout;
const { scrollTop, topOffset } = viewport();
const zIndex = 1000 + z;
const zIndex = z + 1000;

if (layout.scrolling) {
cssProps = {
Expand Down
16 changes: 8 additions & 8 deletions src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
IProcessedStickyLayout,
IStickyBehavior,
IStickyHandle,
IZIndexCalculation,
updateStickyLayout,
} from "./calc";
import {
Expand Down Expand Up @@ -68,8 +69,8 @@ export const StickyContainer: FC<PropsWithChildren<{}>> = ({ children }) => {
};

export interface IStickyProps extends HTMLAttributes<HTMLDivElement> {
defaultZIndex?: number;
behavior: IStickyBehavior;
zIndex?: IZIndexCalculation | number;
labels?: ILabels;
respondsTo?: ISelectorFunction;
}
Expand All @@ -79,40 +80,39 @@ const placeholderStyle = { display: "block", position: "relative" };

export const Sticky: FC<PropsWithChildren<IStickyProps>> = memo(
({
zIndex,
behavior,
children,
labels,
respondsTo,
defaultZIndex,
...attributes
}) => {
const behaviorState = useRef<any>({});
const placeholderRef = useRef<HTMLElement>();
let ref: RefObject<HTMLElement>;

const handle: IStickyHandle = {
behavior,
labels,
selectorFunction: respondsTo,
behaviorState: behaviorState.current,
placeholderRef,
update: (sticky, stickyCssProps) => {
update: (isSticky, cssProps, layout) => {
const wrapper = ref.current;
const placeholder = placeholderRef.current;
if (!wrapper || !placeholder) {
return;
}
const wrapperCssProps: ICssStyleData = {
...wrapperStyle,
...stickyCssProps,
...(!sticky &&
defaultZIndex !== undefined && {
zIndex: defaultZIndex,
}),
...cssProps,
zIndex: typeof zIndex === 'undefined' ? cssProps.zIndex : typeof zIndex === 'function' ? zIndex(isSticky, cssProps, layout) : zIndex,
};

for (const k of Object.keys(wrapperCssProps)) {
wrapper.style[k as any] = wrapperCssProps[k];
}

placeholder.style.height = wrapper.offsetHeight + "px";
wrapper.style.width = placeholder.offsetWidth + "px";
},
Expand Down