-
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ refactor: dumi preview action bar
- Loading branch information
1 parent
b473aa2
commit 8dbe3ac
Showing
10 changed files
with
203 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
packages/dumi-theme-lobehub/src/slots/PreviewerActions/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
packages/dumi-theme-lobehub/src/slots/PreviewerActions/style.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | ||
`, | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | ||
} | ||
`, | ||
), | ||
}; | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8dbe3ac
There was a problem hiding this comment.
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