Skip to content

Commit

Permalink
feat: Add an additional 'sourceState' props attribute to the component (
Browse files Browse the repository at this point in the history
#236)

* fix: fixed up ts error on `/website`

* feat(deps): 增加@types react|react-dom to ^18.2.0 version

* feat: Add an additional 'sourceState' props attribute to the component.

* fix: fixed up eslint error

* fix: Option 'emitDeclarationOnly' cannot be specified with option 'noEmit'.
  • Loading branch information
LonelyFellas committed Mar 25, 2024
1 parent 97e4c6c commit e6f094d
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/index.tsx
Expand Up @@ -71,6 +71,10 @@ export interface CodePreviewProps extends SplitProps {
* @default light
*/
theme?: ReactCodeMirrorProps['theme'];
/**
* Specifies the initial state of the source panel.
*/
sourceState?: 'hidden' | 'shown';
}

export interface CodePreviewState {
Expand Down
5 changes: 3 additions & 2 deletions src/useCodePreview.ts
Expand Up @@ -16,12 +16,13 @@ export const getReactDOMClient = () => {
};

export function useCodePreview(props: CodePreviewProps) {
const isShowEdit = props.sourceState === 'shown';
const [demoDom, setDemoDom] = useState<HTMLDivElement>();
const playerId = useRef(`${parseInt(String(Math.random() * 1e9), 10).toString(36)}`);
const [fullScreen, setFullScreen] = useState(false);
const [errorMessage, setErrorMessage] = useState('');
const [showEdit, setShowEdit] = useState(false);
const [width, setWidth] = useState<number | string>(1);
const [showEdit, setShowEdit] = useState(isShowEdit);
const [width, setWidth] = useState<number | string>(isShowEdit ? '50%' : 1);
const [copied, setCopied] = useState(false);
const [code, setCode] = useState(props.code || '');

Expand Down
1 change: 1 addition & 0 deletions test/index.test.tsx
Expand Up @@ -7,6 +7,7 @@ describe('<CodePreview />', () => {
const component = TestRenderer.create(<CodePreview />);
let tree = component.toJSON();
if (tree && !Array.isArray(tree)) {
/* eslint-disable jest/no-conditional-expect */
expect(tree.type).toEqual('div');
expect(tree.props.className).toEqual('w-split w-code-preview w-code-preview-bordered w-split-horizontal');
expect(tree.props.style).toEqual({
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Expand Up @@ -19,8 +19,8 @@
"noUnusedLocals": true,
"experimentalDecorators": true,
"removeComments": false,
"noEmit": true,
"esModuleInterop": true
"esModuleInterop": true,
"emitDeclarationOnly": true
},
"include": ["src"]
}
1 change: 0 additions & 1 deletion website/App.tsx
@@ -1,4 +1,3 @@
import React from 'react';
import GitHubCorners from '@uiw/react-github-corners';
import Markdown from '@uiw/react-markdown-preview';
import '@wcj/dark-mode';
Expand Down

0 comments on commit e6f094d

Please sign in to comment.