Skip to content

Commit 96c1582

Browse files
committed
feat: add background props.
1 parent 1d962fb commit 96c1582

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

core/README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,25 @@ const Demo = () => (
7171
export default Demo;
7272
```
7373

74+
## Preview Background Color
75+
76+
```jsx mdx:preview
77+
import React from "react"
78+
import CodeLayout from "react-code-preview-layout"
79+
const code = `import React from "react";\nimport { Button } from "uiw";\nconst Demo = ()=>{\n return<div><Button>按钮</Button></div>\n};\nexport default Demo;`
80+
const Demo = () => (
81+
<CodeLayout
82+
code={<code>{code}</code>}
83+
text={code}
84+
bordered={false}
85+
background="#009688b0"
86+
>
87+
<div>示例内容</div>
88+
</CodeLayout>
89+
);
90+
export default Demo;
91+
```
92+
7493
## 自定义操作按钮
7594

7695
```jsx mdx:preview?title=自定义操作按钮
@@ -183,9 +202,11 @@ interface CodeLayoutProps extends React.DetailedHTMLProps<React.HTMLAttributes<H
183202
toolbar?: React.ReactNode;
184203
/** 额外内容,展示 toolbar 右侧内容 */
185204
toolbarExtra?: React.ReactNode;
186-
disableTollbar?: boolean;
205+
disableToolbar?: boolean;
187206
disableCode?: boolean;
188207
disablePreview?: boolean;
208+
/** 禁用方格背景 */
209+
disableCheckered?: boolean;
189210
/**
190211
* 是否需要边框
191212
* @default true

core/src/index.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import './styles.css';
55

66
export interface CodeLayoutProps extends React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement> {
77
prefixCls?: string;
8-
/** 原始 代码块 渲染**/
8+
/** 原始 代码块 渲染 **/
99
code?: React.ReactNode;
1010
text?: string;
11-
/** 标题部分,也可以放置按钮 **/
11+
/** Title section, you can also place buttons **/
1212
toolbar?: React.ReactNode;
1313
/** 额外内容,展示 toolbar 右侧内容 */
1414
toolbarExtra?: React.ReactNode;
@@ -17,6 +17,8 @@ export interface CodeLayoutProps extends React.DetailedHTMLProps<React.HTMLAttri
1717
disablePreview?: boolean;
1818
/** 禁用方格背景 */
1919
disableCheckered?: boolean;
20+
/** Configure the preview background color. */
21+
background?: string;
2022
/**
2123
* 是否需要边框
2224
* @default true
@@ -43,6 +45,7 @@ const CodeLayout = forwardRef<HTMLDivElement, CodeLayoutProps>((props, ref) => {
4345
disableCode = false,
4446
disableToolbar = false,
4547
text = '',
48+
background = '',
4649
copied = true,
4750
toolbarExtra,
4851
code,
@@ -55,9 +58,19 @@ const CodeLayout = forwardRef<HTMLDivElement, CodeLayoutProps>((props, ref) => {
5558
.join(' ')
5659
.trim();
5760

61+
const style: React.CSSProperties = !background
62+
? {}
63+
: {
64+
backgroundColor: background,
65+
backgroundImage: 'none',
66+
};
5867
return (
5968
<div ref={ref} {...other} className={cls}>
60-
{!disablePreview && <div className={`${prefixCls}-preview`}>{children}</div>}
69+
{!disablePreview && (
70+
<div className={`${prefixCls}-preview`} style={style}>
71+
{children}
72+
</div>
73+
)}
6174
{!disableToolbar && (
6275
<div className={`${prefixCls}-toolbar`}>
6376
<div className={`${prefixCls}-title`}>{toolbar}</div>

core/src/styles.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
border-radius: 5px;
2222
background-color: var(--color-canvas-subtle);
2323
}
24+
.w-rcpl-preview {
25+
border-radius: 5px 5px 0 0;
26+
}
2427
.w-rcpl-code.w-hidden {
2528
height: 0;
2629
padding: 0;

0 commit comments

Comments
 (0)