Skip to content

Commit

Permalink
♻️ refactor: dumi preview action bar
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 committed Jun 6, 2023
1 parent b473aa2 commit 8dbe3ac
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 64 deletions.
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ hero:
title: Lobe UI
description: Lobe UI is an open-source UI component library for building chatbot web apps
actions:
\- text: Get Started
text: Get Started
link: /components

---
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"devDependencies": {
"@ant-design/colors": "^7",
"@commitlint/cli": "^17",
"@react-spring/web": "^9",
"@testing-library/react": "^14",
"@types/chroma-js": "^2",
"@types/lodash-es": "^4",
Expand Down
96 changes: 96 additions & 0 deletions packages/dumi-theme-lobehub/src/slots/PreviewerActions/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { ActionIcon, TabsNav } from '@lobehub/ui';
import { type IPreviewerProps,openCodeSandbox, openStackBlitz, useIntl } from 'dumi';
import SourceCode from 'dumi/theme/builtins/SourceCode';
import { Code, Code2, Codesandbox, MonitorUp, Zap } from 'lucide-react';
import { type FC, type ReactNode,useState } from 'react';

import { useStyles } from './style';

const SIZE = { fontSize: 16, strokeWidth: 2, blockSize: 24 };

export interface IPreviewerActionsProps extends IPreviewerProps {
demoContainer: HTMLDivElement | HTMLIFrameElement;
/**
* disabled actions
*/
disabledActions?: ('CSB' | 'STACKBLITZ' | 'EXTERNAL' | 'HTML2SKETCH')[];
extra?: ReactNode;
forceShowCode?: boolean;
}

const PreviewerActions: FC<IPreviewerActionsProps> = (props) => {
const intl = useIntl();
const files = Object.entries(props.asset.dependencies).filter(([, { type }]) => type === 'FILE');
const [activeKey, setActiveKey] = useState(0);
const [showCode, setShowCode] = useState(props.forceShowCode || props.defaultShowCode);
const isSingleFile = files.length === 1;
const lang = (files[activeKey][0].match(/\.([^.]+)$/)?.[1] || 'text') as any;
const { styles } = useStyles();

return (
<>
<div className={styles.actionBar}>
{!props.disabledActions?.includes('CSB') && (
<ActionIcon
icon={Codesandbox}
onClick={() => openCodeSandbox(props)}
size={SIZE}
title={intl.formatMessage({
id: 'previewer.actions.codesandbox',
})}
/>
)}
{!props.disabledActions?.includes('STACKBLITZ') && (
<ActionIcon
icon={Zap}
onClick={() => openStackBlitz(props)}
size={SIZE}
title={intl.formatMessage({
id: 'previewer.actions.stackblitz',
})}
/>
)}
{!props.disabledActions?.includes('EXTERNAL') && (
<a href={props.demoUrl} rel="noreferrer" target="_blank">
<ActionIcon
icon={MonitorUp}
size={SIZE}
title={intl.formatMessage({
id: 'previewer.actions.separate',
})}
/>
</a>
)}
{!props.forceShowCode && (
<ActionIcon
icon={showCode ? Code2 : Code}
onClick={() => setShowCode((prev) => !prev)}
size={SIZE}
title={intl.formatMessage({
id: `previewer.actions.code.${showCode ? 'shrink' : 'expand'}`,
})}
/>
)}
</div>
{showCode && (
<>
<div className={styles.tabs}>
{!isSingleFile && (
<TabsNav
activeKey={String(activeKey)}
items={files.map(([filename], i) => ({
key: String(i),
label: filename,
}))}
onChange={(key) => setActiveKey(Number(key))}
/>
)}
</div>
<SourceCode lang={lang}>{files[activeKey][1].value}</SourceCode>
</>
)}
</>
);
};

export default PreviewerActions;
20 changes: 20 additions & 0 deletions packages/dumi-theme-lobehub/src/slots/PreviewerActions/style.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createStyles } from 'antd-style';

export const useStyles = createStyles(({ token, css }) => {
return {
actionBar: css`
display: flex;
gap: 8px;
align-items: center;
justify-content: center;
height: 36px;
`,
tabs: css`
padding: 0 12px;
background: ${token.colorBgLayout};
border-top: 1px dashed ${token.colorBorderSecondary};
border-bottom: 1px dashed ${token.colorBorderSecondary};
`,
};
});
7 changes: 7 additions & 0 deletions src/ActionIcon/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export interface ActionIconProps extends DivProps {
* @type LucideIcon
*/
icon: LucideIcon;
/**
* @description Handle click action
*/
onClick?: () => void;
/**
* @description The position of the tooltip relative to the target
* @enum ["top","left","right","bottom","topLeft","topRight","bottomLeft","bottomRight","leftTop","leftBottom","rightTop","rightBottom"]
Expand All @@ -50,6 +54,7 @@ export interface ActionIconProps extends DivProps {
* @default 'normal'
*/
size?: ActionIconSize;

/**
* @description The text shown in the tooltip
*/
Expand All @@ -67,6 +72,7 @@ const ActionIcon = memo<ActionIconProps>(
title,
placement,
arrow = false,
onClick,
...props
}) => {
const { styles, cx } = useStyles({ active: Boolean(active), glass: Boolean(glass) });
Expand Down Expand Up @@ -99,6 +105,7 @@ const ActionIcon = memo<ActionIconProps>(
const actionIconBlock = (
<div
className={cx(styles.block, className)}
onClick={onClick}
style={{ width: blockSize, height: blockSize, borderRadius, ...style }}
{...props}
>
Expand Down
3 changes: 2 additions & 1 deletion src/ActionIcon/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const useStyles = createStyles(
background: ${active ? token.colorFillTertiary : 'transparent'};
transition: color 600ms ${token.motionEaseOut},
transition: color 600ms ${token.motionEaseOut}, transform 400ms ${token.motionEaseOut},
background-color 100ms ${token.motionEaseOut};
&:hover {
Expand All @@ -26,6 +26,7 @@ export const useStyles = createStyles(
}
&:active {
transform: scale(0.8);
color: ${token.colorText};
background-color: ${token.colorFill};
}
Expand Down
10 changes: 8 additions & 2 deletions src/Highlighter/SyntaxHighlighter/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ export const useStyles = createStyles(({ css, token, cx, prefixCls, stylish }) =
top: 0;
right: 0;
padding: 4px 8px;
display: flex;
align-items: center;
justify-content: center;
height: 36px;
padding: 0 8px;
font-family: ${token.fontFamilyCode};
color: ${token.colorTextTertiary};
border-radius: ${token.borderRadiusSM};
border-radius: ${token.borderRadius};
`,
),
};
Expand Down
19 changes: 10 additions & 9 deletions src/Highlighter/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@ export const useStyles = createStyles(
const buttonHoverCls = `${prefix}-hover-btn`;
const langHoverCls = `${prefix}-hover-lang`;

const typeStylish = css`
background-color: ${type === 'block' ? token.colorFillTertiary : 'transparent'};
border: 1px solid ${type === 'block' ? 'transparent' : token.colorBorder};
&:hover {
background-color: ${type === 'prue' ? 'transparent' : token.colorFillTertiary};
}
`;

return {
container: cx(
prefix,
typeStylish,
css`
position: relative;
overflow: auto;
background-color: ${type === 'block' ? token.colorFillTertiary : 'transparent'};
border-radius: ${token.borderRadius}px;
transition: background-color 100ms ${token.motionEaseOut};
${type === 'ghost' &&
css`
border: 1px solid ${token.colorBorder};
`}
&:hover {
background-color: ${type === 'prue' ? 'transparent' : token.colorFillTertiary};
.${buttonHoverCls} {
opacity: 1;
}
Expand Down
89 changes: 48 additions & 41 deletions src/Input/style.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
import { createStyles } from 'antd-style';

export const useStyles = createStyles(({ css, token }, { type }: { type: 'ghost' | 'block' }) => ({
input: css`
position: relative;
max-width: 100%;
height: 36px;
padding: 0 12px;
background-color: ${type === 'block' ? token.colorFillTertiary : 'transparent'};
border: 1px solid ${type === 'block' ? 'transparent' : token.colorBorder};
transition: background-color 100ms ${token.motionEaseOut};
input {
background: transparent;
}
&:hover {
background-color: ${token.colorFillTertiary};
}
`,
textarea: css`
position: relative;
max-width: 100%;
padding: 8px 12px;
background-color: ${type === 'block' ? token.colorFillTertiary : 'transparent'};
border: 1px solid ${type === 'block' ? 'transparent' : token.colorBorder};
transition: background-color 100ms ${token.motionEaseOut};
textarea {
background: transparent;
}
&:hover {
background-color: ${token.colorFillTertiary};
}
`,
}));
export const useStyles = createStyles(
({ cx, css, token }, { type }: { type: 'ghost' | 'block' }) => {
const typeStylish = css`
background-color: ${type === 'block' ? token.colorFillTertiary : 'transparent'};
border: 1px solid ${type === 'block' ? 'transparent' : token.colorBorder};
`;

return {
input: cx(
typeStylish,
css`
position: relative;
max-width: 100%;
height: 36px;
padding: 0 12px;
transition: background-color 100ms ${token.motionEaseOut};
input {
background: transparent;
}
&:hover {
background-color: ${token.colorFillTertiary};
}
`,
),
textarea: cx(
typeStylish,
css`
position: relative;
max-width: 100%;
padding: 8px 12px;
transition: background-color 100ms ${token.motionEaseOut};
textarea {
background: transparent;
}
&:hover {
background-color: ${token.colorFillTertiary};
}
`,
),
};
},
);
20 changes: 10 additions & 10 deletions src/Snippet/style.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { createStyles } from 'antd-style';

export const useStyles = createStyles(
({ css, token }, type: 'ghost' | 'block') =>
export const useStyles = createStyles(({ css, cx, token }, type: 'ghost' | 'block') => {
const typeStylish = css`
background-color: ${type === 'block' ? token.colorFillTertiary : 'transparent'};
border: 1px solid ${type === 'block' ? 'transparent' : token.colorBorder};
`;

return cx(
typeStylish,
css`
position: relative;
Expand All @@ -16,13 +22,6 @@ export const useStyles = createStyles(
border-radius: ${token.borderRadius}px;
transition: background-color 100ms ${token.motionEaseOut};
${type === 'block'
? css`
background-color: ${token.colorFillTertiary};
`
: css`
border: 1px solid ${token.colorBorder};
`}
&:hover {
background-color: ${token.colorFillTertiary};
Expand All @@ -47,4 +46,5 @@ export const useStyles = createStyles(
background: none !important;
}
`,
);
);
});

1 comment on commit 8dbe3ac

@vercel
Copy link

@vercel vercel bot commented on 8dbe3ac Jun 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lobe-ui – ./

lobe-ui-git-master-lobehub.vercel.app
lobe-ui.vercel.app
lobe-ui-lobehub.vercel.app
ui.lobehub.com

Please sign in to comment.