Skip to content

Commit

Permalink
[Doc] support doc home page Carousel component (DataLinkDC#3328)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zzm0809 authored Mar 26, 2024
1 parent b0bf25f commit 0eec1d4
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 11 deletions.
4 changes: 2 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
"docusaurus-plugin-less": "^2.0.2",
"less-loader": "10.2.0",
"prism-react-renderer": "^1.3.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react": "^18.2.0",
"react-dom": "^18.0.0",
"yarn": "^1.22.19"
},
"browserslist": {
Expand Down
54 changes: 54 additions & 0 deletions docs/src/components/Carousel/carousel.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
.container {
overflow: hidden;
}

.inner {
white-space: nowrap;
transition: transform 0.3s;
}

.carousel_item {
display: inline-flex;
align-items: center;
justify-content: center;
height: 200px;
color: #fff;
background-color: #312520;
}

.loading {
position: absolute;
bottom: -25px;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
margin-bottom: 10px;
width: 100%;
}

.indicator_outer {
width: 90px;
height: 7px;
background-color: #0000001a;
margin-left: 20px;
border-radius: 5px;
}

.indicator_inside {
height: 100%;
border-radius: 5px;
animation-fill-mode: forwards;
animation-name: progressBar;
animation-iteration-count: infinite;
}

@keyframes progressBar {
0% {
width: 0;
}
100% {
width: 100%;
}
}

161 changes: 161 additions & 0 deletions docs/src/components/Carousel/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import React, {useEffect, useState} from "react";
import style from "./carousel.module.css";
import {CarouselItemInfo} from "../CarouselList";

/**
* @param {children} children ReactNode
* @param {width} width 宽度
* @param {height} height 高度
* @param {styles} styles 样式
* @returns 轮播图 单项
*/
type CarouselItemProps = {
children: React.ReactNode;
width?: string | number;
height?: string | number;
styles?: React.CSSProperties;
}
export const CarouselItem = (props: CarouselItemProps) => {
const {children, width = "100%", height = "100%", styles = {}} = props;
return (
<div
className={style.carousel_item}
style={{width: width, height: height, ...styles}}
>
{children}
</div>
);
};

/**
* @returns 轮播图 主体
* @param props
*/
export const CarouselInfo: React.FC<CarouselItemInfo> = (props: { item: CarouselItemInfo; }) => {
const {item} = props;
return (
<div className="carousel_info_container">
{/*<div className="carousel_info_info">*/}
{/* <h1>{item.title}</h1>*/}
{/* <span>{item.describe}</span>*/}
{/*</div>*/}
<div className="carousel_info_image_container">
<img src={item.image} alt="Jay" className="carousel_info_image"/>
</div>
</div>
);
};

/**
* @param {children} children ReactNode
* @param {switchingTime} switchingTime 间隔时间 默认3秒 以毫秒为单位 3000ms = 3s
* @returns 轮播图 容器
*/
const Carousel = ({
children = React.createElement("div"),
switchingTime = 3000,
}) => {
const time = ((switchingTime % 60000) / 1000).toFixed(0); // 将毫秒转换为秒
const [activeIndex, setActiveIndex] = useState(0); // 对应索引

/**
* 更新索引
* @param {newIndex} newIndex 更新索引
*/
const onUpdateIndex = (newIndex: React.SetStateAction<number>) => {
if (newIndex < 0) {
newIndex = React.Children.count(children) - 1;
} else if (newIndex >= React.Children.count(children)) {
newIndex = 0;
}
setActiveIndex(newIndex);
replayAnimations();
};

/**
* 重置动画
*/
const replayAnimations = () => {
document.getAnimations().forEach((anim) => {
anim.cancel();
anim.play();
});
};

/**
* 底部加载条点击事件
* @param {index} index 跳转索引
*/
const onClickCarouselIndex = (index: React.SetStateAction<number>) => {
onUpdateIndex(index);
replayAnimations();
};

useEffect(() => {
const interval = setInterval(() => {
onUpdateIndex(activeIndex + 1);
}, switchingTime);

return () => {
if (interval) {
clearInterval(interval);
}
};
});

/**
* Renders the children elements with specific styles and transformations.
*
* @return {JSX.Element} The rendered JSX elements with modified styles.
*/
const renderChildren = () => {
return (
<div
className={style.inner}
style={{transform: `translateX(-${activeIndex * 100}%)`}}
>
{React.Children.map(children, (child) => {
return React.cloneElement(child, {width: "100%", height: "100%"});
})}
</div>
)
}

/**
* Function to render loading indicators for each child component.
*
* @return {JSX.Element} The loading indicators JSX element
*/
const renderLoading = () => {
return (
<div className={style.loading}>
{React.Children.map(children, (child, index) => {
return (
<div
className={style.indicator_outer}
onClick={() => onClickCarouselIndex(index)}
>
<div
className={style.indicator_inside}
style={{
animationDuration: index === activeIndex ? `${time}s` : "0s",
backgroundColor: index === activeIndex ? "#041d81" : null,
}}
/>
</div>
);
})}
</div>
)
}

return (
<div className={style.container}>
{renderChildren()}
{renderLoading()}
</div>
);
};

export default Carousel;

63 changes: 63 additions & 0 deletions docs/src/components/CarouselList/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import Carousel, {CarouselInfo, CarouselItem} from "../Carousel";
import React from "react";


/**
* @param {id} id 轮播图id
* @param {title} title 标题
* @param {describe} describe 描述
* @param {image} image 图片
* @param {imageWidth} imageWidth 图片宽度
* @param {imageHeight} imageHeight 图片高度
* @param {backgroundColor} backgroundColor 背景颜色
* @param {jumpUrl} jumpUrl 跳转链接
* @returns 轮播图信息
* @constructor
*/
export interface CarouselItemInfo {
id: number;
title?: string;
describe?: string;
image: string | React.ReactNode;
imageWidth?: string | number;
imageHeight?: string | number;
backgroundColor?: string;
jumpUrl?: string;
}

/**
* @param {items} items 轮播图列表
*/
type CarouselItemProps = {
items: CarouselItemInfo[];
}

/**
* @returns 轮播图列表
* @param props
* @constructor
*/
const CarouselList:React.FC<CarouselItemProps> = (props: { items: CarouselItemInfo[]; }) => {

const {items} = props

return (
<Carousel>
{items?.map((item :CarouselItemInfo)=> {
return (
<CarouselItem
key={item.id}
width={item.imageWidth}
height={item.imageHeight}
// styles={{ backgroundColor: item.backgroundColor }}
>
<CarouselInfo item={item}/>
</CarouselItem>
);
})}
</Carousel>
);
};

export default CarouselList;

11 changes: 9 additions & 2 deletions docs/src/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ div[class^='announcementBar_'] {
text-decoration:none;
}

.container{
width: 100%;
margin-right: 50vw;
}

.container .desc{
width: 100%;
Expand All @@ -269,6 +273,10 @@ div[class^='announcementBar_'] {
}
.container .desc_left{
width:50%;
display:flex;
flex-direction: column;
align-content: center;
align-items: center;
}
.container .desc_left .hero__title{
font-size:2.5rem;
Expand All @@ -291,8 +299,7 @@ div[class^='announcementBar_'] {
display:flex;
position: relative;
width:100%;
height: 20%;
padding-left: 80px;
height:100%;
}
.container .desc .desc_right img .fly_svg{
width:100%;
Expand Down
45 changes: 39 additions & 6 deletions docs/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,33 @@ import Link from '@docusaurus/Link';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import styles from './index.module.css';
import HomepageFeatures from '@site/src/components/HomepageFeatures';
import CarouselList from "../components/CarouselList";


// 轮播图数据
const info = [
{
id: 1,
image: 'https://pic.dinky.org.cn/dinky/docs/zh-CN/home/datastudio.png',
},
{
id: 2,
image: 'https://pic.dinky.org.cn/dinky/docs/zh-CN/home/checksql.png',
},
{
id: 3,
image: 'https://pic.dinky.org.cn/dinky/docs/zh-CN/home/versiondiff.png',
},
{
id: 4,
image: 'https://pic.dinky.org.cn/dinky/docs/zh-CN/home/lineage.png',
},
{
id: 5,
image: 'https://pic.dinky.org.cn/dinky/docs/zh-CN/home/monitor.png',
},
];

function HomepageHeader() {
const {siteConfig} = useDocusaurusContext();
return (
Expand Down Expand Up @@ -75,18 +100,26 @@ function HomepageHeader() {
</div>

<div className={styles.buttonLink}>
<iframe style={{width:"100px",height:"30px"}}
<iframe style={{width:"150px",height:"30px"}}
src="https://ghbtns.com/github-btn.html?user=DataLinkDC&amp;repo=dinky&amp;type=star&amp;count=true&amp;size=large"
title="GitHub Stars"
/>
</div>
</div>
</div>
<div className={clsx("desc_right", styles.box,styles.descRight)}>
<div>
<img src="home.png" className="fly_svg"></img>
</div>
</div>
{/*<div className={clsx("desc_right", styles.box,styles.descRight)}>*/}
{/* <div>*/}
{/* <img src="home.png" className="fly_svg"></img>*/}
{/* </div>*/}
{/*</div>*/}
<div style={{
position: "absolute",
width: "40vw",
right: "10vw",
zIndex: "1"
}}>
<CarouselList items={info}/>
</div>
</div>
</div>
</header>
Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/index.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
.buttons {
display: flex;
align-items: center;
justify-content: space-around;
justify-content: center;
width: 30rem;
height: 10rem;
}
Expand Down

0 comments on commit 0eec1d4

Please sign in to comment.