Skip to content

Commit

Permalink
💄 style: update SyntaxHighlighter and form
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 committed Jul 4, 2023
1 parent 7a8e5a5 commit 4adfbfa
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 17 deletions.
103 changes: 103 additions & 0 deletions src/Form/demos/Data.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { Form, type FormProps } from '@lobehub/ui';
import { Button, InputNumber, Segmented, Select, Switch } from 'antd';
import { Palette, PanelLeftClose } from 'lucide-react';

const setting = {
i18n: 'en',
liteAnimation: false,
sidebarExpand: true,
sidebarFixedMode: 'float',
sidebarWidth: 300,
};

const items: FormProps['items'] = [
{
children: [
{
children: (
<Select
options={[
{
label: 'English',
value: 'en',
},
{
label: '简体中文',
value: 'zh_CN',
},
]}
/>
),
desc: 'Editor language',
label: 'Language',
name: 'i18n',
},
{
children: <Switch />,
desc: 'Reduce the blur effect and background flow color, which can improve smoothness and save CPU usage',
label: 'Reduce Animation',
name: 'liteAnimation',
valuePropName: 'checked',
},
],
icon: Palette,
title: 'Theme Settings',
},
{
children: [
{
children: <Switch />,
desc: 'Whether to expand the sidebar by default when starting',
label: 'Default Expand',
name: 'sidebarExpand',
valuePropName: 'checked',
},
{
children: (
<Segmented
options={[
{
label: 'Fixed',
value: 'fixed',
},
{
label: 'Float',
value: 'float',
},
]}
/>
),
desc: 'Fixed as grid mode for constant display, auto-expand when the mouse moves to the side in floating mode',
label: 'Display Mode',
name: 'sidebarFixedMode',
},
{
children: <InputNumber />,
desc: 'Default width of the sidebar when starting',
label: 'Default Width',
name: 'sidebarWidth',
},
],
icon: PanelLeftClose,
title: 'Quick Setting Sidebar',
},
];

export default () => {
return (
<Form
footer={
<>
<Button htmlType="button">Reset</Button>
<Button htmlType="submit" type="primary">
Submit
</Button>
</>
}
initialValues={setting}
items={items}
onFinish={console.table}
style={{ width: 500 }}
/>
);
};
2 changes: 1 addition & 1 deletion src/Form/demos/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const setting = {

export default () => {
return (
<Form initialValues={setting} style={{ width: 500 }}>
<Form initialValues={setting} onFinish={console.table} style={{ width: 500 }}>
<FormGroup icon={Palette} title={'Theme Settings'}>
<FormItem desc={'Editor language'} label={'Language'} name="i18n">
<Select
Expand Down
4 changes: 4 additions & 0 deletions src/Form/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ description: High performance Form component with data scope management. Includi

<code src="./demos/index.tsx" center></code>

## DataDrive

<code src="./demos/Data.tsx" center></code>

## APIs

<API></API>
33 changes: 28 additions & 5 deletions src/Form/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
import { Form as AntForm, FormProps as AntFormProps } from 'antd';
import { memo } from 'react';
import { LucideIcon } from 'lucide-react';
import { type ReactNode, memo } from 'react';

import FormFooter from './components/FormFooter';
import FormGroup from './components/FormGroup';
import FormItem, { FormItemProps } from './components/FormItem';
import { useStyles } from './style';

export type FormProps = AntFormProps;
export interface ItemGroup {
children: FormItemProps[];
icon: LucideIcon;
title: string;
}

const Form = memo<FormProps>(({ className, ...props }) => {
export interface FormProps extends AntFormProps {
children?: ReactNode;
footer?: ReactNode;
items?: ItemGroup[];
}

const Form = memo<FormProps>(({ className, footer, items, children, ...props }) => {
const { cx, styles } = useStyles();
return (
// @ts-ignore
<AntForm className={cx(styles.form, className)} colon={false} layout="horizontal" {...props} />
<AntForm className={cx(styles.form, className)} colon={false} layout="horizontal" {...props}>
{items?.map((group, groupIndex) => (
<FormGroup icon={group.icon} key={groupIndex} title={group.title}>
{group.children.map((item, itemIndex) => {
return <FormItem divider={itemIndex !== 0} key={itemIndex} {...item} />;
})}
</FormGroup>
))}
{children}
{footer && <FormFooter>{footer}</FormFooter>}
</AntForm>
);
});
export default Form;
15 changes: 10 additions & 5 deletions src/Highlighter/SyntaxHighlighter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@ import { useThemeMode } from 'antd-style';
import { Loader2 } from 'lucide-react';
import { memo, useEffect } from 'react';
import { Center } from 'react-layout-kit';
import { HighlighterOptions } from 'shiki-es';
import { shallow } from 'zustand/shallow';

import Icon from '@/Icon';
import { useHighlight } from '@/hooks/useHighlight';

import type { HighlighterProps } from '../index';
import { useStyles } from './style';

export type SyntaxHighlighterProps = Pick<HighlighterProps, 'language' | 'children' | 'theme'>;
export interface SyntaxHighlighterProps {
children: string;
language: string;
options?: HighlighterOptions;
theme?: 'dark' | 'light';
}

const SyntaxHighlighter = memo<SyntaxHighlighterProps>(({ children, language }) => {
const SyntaxHighlighter = memo<SyntaxHighlighterProps>(({ children, language, options }) => {
const { styles } = useStyles();
const { isDarkMode } = useThemeMode();
const [codeToHtml, isLoading] = useHighlight((s) => [s.codeToHtml, !s.highlighter], shallow);

useEffect(() => {
useHighlight.getState().initHighlighter();
}, []);
useHighlight.getState().initHighlighter(options);
}, [options]);

return (
<>
Expand Down
21 changes: 21 additions & 0 deletions src/ThemeProvider/GlobalStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,27 @@ const GlobalStyle = createGlobalStyle`
}
}
}
.ant-tooltip-inner {
display: flex;
align-items: center;
justify-content: center;
min-height: unset;
padding: 4px 8px;
color: ${({ theme }) => theme.colorBgLayout};
background-color: ${({ theme }) => theme.colorText};
border-radius: ${({ theme }) => theme.borderRadiusSM}px;
}
.ant-tooltip-arrow {
&::before,
&::after {
background: ${({ theme }) => theme.colorText};
}
}
`;

export default GlobalStyle;
6 changes: 5 additions & 1 deletion src/ThemeProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface ThemeProviderProps {
* @description Custom extra token
*/
customToken?: (theme: CustomTokenParams) => { [key: string]: any };
enableWebfonts?: boolean;
/**
* @description Whether to inline the styles on server-side rendering or not
*/
Expand All @@ -53,6 +54,7 @@ const ThemeProvider = memo<ThemeProviderProps>(
themeMode,
customStylish = () => ({}),
customToken = () => ({}),
enableWebfonts = true,
webfonts = [
'https://npm.elemecdn.com/@lobehub/webfont-mono/css/index.css',
'https://npm.elemecdn.com/@lobehub/webfont-harmony-sans/css/index.css',
Expand All @@ -63,7 +65,9 @@ const ThemeProvider = memo<ThemeProviderProps>(

return (
<>
{webfonts && webfonts.map((webfont, index) => <FontLoader key={index} url={webfont} />)}
{enableWebfonts &&
webfonts?.length > 0 &&
webfonts.map((webfont, index) => <FontLoader key={index} url={webfont} />)}
<StyleProvider speedy={process.env.NODE_ENV === 'production'}>
<AntdThemeProvider<LobeCustomToken>
customStylish={(theme) => ({ ...lobeCustomStylish(theme), ...customStylish(theme) })}
Expand Down
10 changes: 5 additions & 5 deletions src/hooks/useHighlight.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Highlighter, getHighlighter } from 'shiki-es';
import { type Highlighter, type HighlighterOptions, getHighlighter } from 'shiki-es';
import { create } from 'zustand';

import { themeConfig } from '@/Highlighter/theme';
Expand Down Expand Up @@ -39,7 +39,7 @@ interface Store {
* @title Initialize the highlighter object
* @returns Initialization promise object
*/
initHighlighter: () => Promise<void>;
initHighlighter: (options?: HighlighterOptions) => Promise<void>;
}

export const useHighlight = create<Store>((set, get) => ({
Expand All @@ -59,11 +59,11 @@ export const useHighlight = create<Store>((set, get) => ({
},
highlighter: undefined,

initHighlighter: async () => {
initHighlighter: async (options) => {
if (!get().highlighter) {
const highlighter = await getHighlighter({
langs: languageMap as any,
themes: [themeConfig(true), themeConfig(false)],
langs: options?.langs || (languageMap as any),
themes: options?.themes || [themeConfig(true), themeConfig(false)],
});

set({ highlighter });
Expand Down

0 comments on commit 4adfbfa

Please sign in to comment.