Skip to content

feat: loading template theme support #2920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 26, 2025
Merged
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
2 changes: 1 addition & 1 deletion packages/plots/src/components/base/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const BaseChart: ForwardRefExoticComponent<PropsWithoutRef<CommonConfig>

return (
<ErrorBoundary errorTemplate={errorTemplate}>
{loading && <ChartLoading loadingTemplate={loadingTemplate} />}
{loading && <ChartLoading loadingTemplate={loadingTemplate} theme={config.theme} loading={loading} />}
<div className={className} style={containerStyle} ref={container} {...containerAttributes} />
</ErrorBoundary>
);
Expand Down
27 changes: 16 additions & 11 deletions packages/util/src/rc/chart-loading.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import React from 'react';
import { ContainerConfig } from '../types';

export interface ChartLoadingConfig {
export interface ChartLoadingConfig extends Pick<ContainerConfig, 'loadingTemplate' | 'loading'> {
/**
* @title 主题
* @description 配置主题颜色
*/
theme?: string | object;
/**
* @title 加载模板
* @description 图表加载
*/
loadingTemplate?: React.ReactElement;
theme?: string;
}

const shadowLoading = (ele: HTMLElement) => {
const shadowLoading = (ele: HTMLElement, style = {}) => {
if (typeof document === 'undefined') {
return 'loading';
}
let overStyle = "";
if (style) {
Object.keys(style).forEach((key) => {
overStyle += `${key}: ${style[key]};\n`;
});
}
const shadowRoot = ele.attachShadow({ mode: 'open' });
const shadowDiv = document.createElement('div');
const shadowStyle = document.createElement('style');
Expand All @@ -34,6 +36,7 @@ const shadowLoading = (ele: HTMLElement) => {
border-radius: 50%;
background: #ccc;
animation-timing-function: cubic-bezier(0, 1, 1, 0);
${overStyle}
}
.loading div:nth-child(1) {
left: 8px;
Expand Down Expand Up @@ -82,12 +85,13 @@ const shadowLoading = (ele: HTMLElement) => {
shadowRoot.appendChild(shadowDiv);
};

export const ChartLoading = ({ loadingTemplate, theme = 'light' }: ChartLoadingConfig) => {
export const ChartLoading = ({ loadingTemplate, theme = 'light', loading }: ChartLoadingConfig) => {
const shadow = React.useRef<HTMLDivElement>(null);
const { container = {}, icon = {} } = typeof loading === 'object' ? loading : {};

React.useEffect(() => {
if (!loadingTemplate && shadow.current) {
shadowLoading(shadow.current);
shadowLoading(shadow.current, icon);
}
}, []);
const renderLoading = () => {
Expand All @@ -108,7 +112,8 @@ export const ChartLoading = ({ loadingTemplate, theme = 'light' }: ChartLoadingC
left: 0,
top: 0,
zIndex: 99,
backgroundColor: theme === 'dark' ? 'rgb(20, 20, 20)' : 'rgb(255, 255, 255)',
background: theme === 'dark' ? 'rgb(20, 20, 20)' : 'rgb(255, 255, 255)',
...container
}}
>
{renderLoading()}
Expand Down
12 changes: 11 additions & 1 deletion packages/util/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
export type LoadingStyle = {
/**
* @title icon style
*/
icon?: React.CSSProperties;
/**
* @title container style
*/
container?: React.CSSProperties;
}
export interface ContainerConfig {
/**
* @title 图表样式
Expand Down Expand Up @@ -28,7 +38,7 @@ export interface ContainerConfig {
* @description.en_US Is it loading
* @default.en_US false
*/
loading?: boolean;
loading?: boolean | LoadingStyle;
/**
* @title 加载模板
* @description 加载模板
Expand Down