Skip to content

Commit

Permalink
Merge pull request #612 from easyops-cn/steve/footer-pinned
Browse files Browse the repository at this point in the history
fix(main-view): show footer shadow only when it's pinned
  • Loading branch information
qiaofengxi authored Nov 16, 2023
2 parents 249f59e + 9045955 commit 5686c37
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
35 changes: 32 additions & 3 deletions bricks/containers/src/main-view/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ import { EoMainView } from "./index.js";

jest.mock("@next-core/theme", () => ({}));

const disconnect = jest.fn();
(global.IntersectionObserver as any) = jest.fn((callback: any) => ({
observe() {
callback([{ intersectionRatio: 0.99 }]);
},
disconnect,
}));

describe("eo-main-view", () => {
test("basic usage", async () => {
test("basic usage", () => {
const element = document.createElement("eo-main-view") as EoMainView;

expect(element.shadowRoot).toBeFalsy();
Expand All @@ -26,7 +34,7 @@ describe("eo-main-view", () => {
expect(element.shadowRoot?.childNodes.length).toBe(0);
});

test("narrow", async () => {
test("narrow", () => {
const element = document.createElement("eo-main-view") as EoMainView;
element.narrow = "medium";

Expand All @@ -43,7 +51,7 @@ describe("eo-main-view", () => {
});
});

test("banner alone", async () => {
test("banner alone", () => {
const element = document.createElement("eo-main-view") as EoMainView;
element.bannerAlone = true;
element.bannerTitle = "Hello";
Expand All @@ -65,4 +73,25 @@ describe("eo-main-view", () => {
document.body.removeChild(element);
});
});

test("footer", async () => {
const element = document.createElement("eo-main-view") as EoMainView;

act(() => {
document.body.appendChild(element);
});
expect(
element.shadowRoot?.querySelector(".footer").classList.contains("pinned")
).toBe(false);

element.showFooter = true;
await act(() => (global as any).flushPromises());
expect(
element.shadowRoot?.querySelector(".footer").classList.contains("pinned")
).toBe(true);

act(() => {
document.body.removeChild(element);
});
});
});
31 changes: 29 additions & 2 deletions bricks/containers/src/main-view/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useEffect, useLayoutEffect, useRef, useState } from "react";
import { createDecorators } from "@next-core/element";
import {
ReactNextElement,
Expand All @@ -13,6 +13,7 @@ import type {
} from "../narrow-view/index.js";
import { BannerProps, EoBanner } from "../banner/index.js";
import styleText from "./styles.shadow.css";
import classNames from "classnames";

const { defineElement, property } = createDecorators();

Expand Down Expand Up @@ -111,6 +112,7 @@ class EoMainView extends ReactNextElement {
bannerTitle={this.bannerTitle}
bannerDescription={this.bannerDescription}
bannerImage={this.bannerImage}
showFooter={this.showFooter}
/>
);
}
Expand All @@ -122,9 +124,31 @@ export function EoMainViewComponent({
bannerTitle,
bannerDescription,
bannerImage,
showFooter,
}: MainViewProps) {
const narrow = _narrow ?? "full";
const bannerConfig = bannerAlone ? { bannerTitle, bannerDescription } : null;
const footerRef = useRef<HTMLDivElement>(null);
const [footerPinned, setFooterPinned] = useState(false);

useEffect(() => {
const footer = footerRef.current;
if (showFooter && footer) {
const observer = new IntersectionObserver(
([e]) => {
setFooterPinned(e.intersectionRatio < 1);
},
{
rootMargin: "0px 0px -1px 0px",
threshold: [1],
}
);
observer.observe(footer);
return () => {
observer.disconnect();
};
}
}, [showFooter]);

return (
<>
Expand Down Expand Up @@ -153,7 +177,10 @@ export function EoMainViewComponent({
<WrappedNarrowView className="content" size={narrow}>
<slot />
</WrappedNarrowView>
<div className="footer">
<div
className={classNames("footer", { pinned: footerPinned })}
ref={footerRef}
>
<WrappedNarrowView size={narrow}>
<slot name="footer" />
</WrappedNarrowView>
Expand Down
9 changes: 6 additions & 3 deletions bricks/containers/src/main-view/styles.shadow.css
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,19 @@ eo-banner {
position: sticky;
bottom: 0;
width: 100%;
background: var(--color-fill-global-bg-2);
/* box-shadow: 0px -4px 16px 0px rgba(0,0,0,0.08); */
box-shadow: var(--slide-up-shadow);
margin-left: calc(-1 * var(--page-padding-left));
margin-right: calc(-1 * var(--page-padding-right));
margin-bottom: calc(-1 * var(--page-padding-bottom));
padding-left: var(--page-padding-left);
padding-right: var(--page-padding-right);
}

.pinned {
background: var(--color-fill-global-bg-2);
/* box-shadow: 0px -4px 16px 0px rgba(0,0,0,0.08); */
box-shadow: var(--slide-up-shadow);
}

.footer > eo-narrow-view {
padding: 20px 0;
}
Expand Down

0 comments on commit 5686c37

Please sign in to comment.