@redocly/theme-experimental 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -0
- package/README.md +1 -0
- package/lib/layouts/CodeGuide/CodeDemo.d.ts +2 -0
- package/lib/layouts/CodeGuide/CodeDemo.js +40 -0
- package/lib/layouts/CodeGuide/CodeGuide.d.ts +2 -0
- package/lib/layouts/CodeGuide/CodeGuide.js +39 -0
- package/lib/mocks/CodeGuide/index.d.ts +36 -0
- package/lib/mocks/CodeGuide/index.js +60 -0
- package/package.json +97 -0
- package/src/layouts/CodeGuide/CodeDemo.tsx +53 -0
- package/src/layouts/CodeGuide/CodeGuide.tsx +46 -0
- package/src/mocks/CodeGuide/index.tsx +79 -0
package/LICENSE
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(c) Copyright 2023 Redocly Inc, all rights reserved.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
For more details, contact team@redocly.com.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.CodeDemo = void 0;
|
|
7
|
+
const react_1 = __importDefault(require("react"));
|
|
8
|
+
const styled_components_1 = __importDefault(require("styled-components"));
|
|
9
|
+
const CodeBlock_1 = require("@theme/components/CodeBlock/CodeBlock");
|
|
10
|
+
const CodeGuide_1 = require("../../mocks/CodeGuide");
|
|
11
|
+
function CodeDemo() {
|
|
12
|
+
const { highlightedLines, activeCodeTab, setCurrentCodeSample, setCodeNode, codeGuideFiles } = react_1.default.useContext(CodeGuide_1.CodeGuideContext);
|
|
13
|
+
(0, CodeGuide_1.useCodeGuideHighlightLines)(highlightedLines);
|
|
14
|
+
const codeBlockRef = react_1.default.useRef(null);
|
|
15
|
+
const setRef = (instance) => {
|
|
16
|
+
if (instance) {
|
|
17
|
+
codeBlockRef.current = instance;
|
|
18
|
+
setCodeNode(codeBlockRef);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
const activeCodeExample = codeGuideFiles.find(({ name }) => activeCodeTab === name);
|
|
22
|
+
return (react_1.default.createElement(CodeDemoWrapper, null,
|
|
23
|
+
react_1.default.createElement(CodeBlock_1.CodeBlock, { source: activeCodeExample === null || activeCodeExample === void 0 ? void 0 : activeCodeExample.content, lang: activeCodeExample === null || activeCodeExample === void 0 ? void 0 : activeCodeExample.lang, withLineNumbers: true, header: {
|
|
24
|
+
controls: {
|
|
25
|
+
copy: {},
|
|
26
|
+
},
|
|
27
|
+
}, codeBlockRef: setRef, codeBlockMaxHeight: "75vh", hideCodeColors: true, tabs: {
|
|
28
|
+
files: codeGuideFiles,
|
|
29
|
+
activeTabName: activeCodeTab,
|
|
30
|
+
handleTabSwitch: setCurrentCodeSample,
|
|
31
|
+
} })));
|
|
32
|
+
}
|
|
33
|
+
exports.CodeDemo = CodeDemo;
|
|
34
|
+
const CodeDemoWrapper = styled_components_1.default.div `
|
|
35
|
+
background-color: var(--samples-panel-controls-background-color);
|
|
36
|
+
border-radius: 0 var(--global-border-radius);
|
|
37
|
+
padding: 32px 0;
|
|
38
|
+
min-height: 85vh;
|
|
39
|
+
`;
|
|
40
|
+
//# sourceMappingURL=CodeDemo.js.map
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const react_1 = __importDefault(require("react"));
|
|
7
|
+
const styled_components_1 = __importDefault(require("styled-components"));
|
|
8
|
+
const CodeDemo_1 = require("../../layouts/CodeGuide/CodeDemo");
|
|
9
|
+
const MarkdownWrapper_1 = require("@theme/components/Markdown/MarkdownWrapper");
|
|
10
|
+
const CodeGuide_1 = require("../../mocks/CodeGuide");
|
|
11
|
+
function CodeGuide(props) {
|
|
12
|
+
var _a;
|
|
13
|
+
const { pageProps: { codeGuideFiles }, children, } = props;
|
|
14
|
+
const initialState = {
|
|
15
|
+
codeGuideFiles,
|
|
16
|
+
highlightedLines: '',
|
|
17
|
+
activeCodeTab: ((_a = codeGuideFiles === null || codeGuideFiles === void 0 ? void 0 : codeGuideFiles[0]) === null || _a === void 0 ? void 0 : _a.name) || '',
|
|
18
|
+
};
|
|
19
|
+
return (react_1.default.createElement(CodeGuide_1.CodeGuideProvider, { initialState: initialState },
|
|
20
|
+
react_1.default.createElement(Wrapper, { "data-component-name": "layouts/CodeGuide" },
|
|
21
|
+
react_1.default.createElement(MarkdownWrapper_1.MarkdownWrapper, null, children),
|
|
22
|
+
react_1.default.createElement("div", null,
|
|
23
|
+
react_1.default.createElement(StickyPosition, null,
|
|
24
|
+
react_1.default.createElement(CodeDemo_1.CodeDemo, null))))));
|
|
25
|
+
}
|
|
26
|
+
exports.default = CodeGuide;
|
|
27
|
+
const Wrapper = styled_components_1.default.div `
|
|
28
|
+
padding-left: 24px;
|
|
29
|
+
display: grid;
|
|
30
|
+
grid-template-columns: 40% 55%;
|
|
31
|
+
grid-column-gap: 45px;
|
|
32
|
+
height: 100%;
|
|
33
|
+
`;
|
|
34
|
+
const StickyPosition = styled_components_1.default.div `
|
|
35
|
+
position: sticky;
|
|
36
|
+
top: 50px;
|
|
37
|
+
left: 0;
|
|
38
|
+
`;
|
|
39
|
+
//# sourceMappingURL=CodeGuide.js.map
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export declare const CodeGuideContext: React.Context<{
|
|
3
|
+
highlightedLines: string;
|
|
4
|
+
activeCodeTab: string;
|
|
5
|
+
activeDocsSection: string;
|
|
6
|
+
codeBlock: undefined;
|
|
7
|
+
codeGuideFiles: CodeGuideFile[];
|
|
8
|
+
updateHighlightedLines: (_lines: string) => void;
|
|
9
|
+
setCurrentCodeSample: (_fileName: string) => void;
|
|
10
|
+
setActiveDocumentationSection: (_sectionId: string) => void;
|
|
11
|
+
setCodeNode: (_node: React.MutableRefObject<HTMLPreElement | null>) => void;
|
|
12
|
+
}>;
|
|
13
|
+
export type CodeGuideFile = {
|
|
14
|
+
name: string;
|
|
15
|
+
lang?: string;
|
|
16
|
+
content: string;
|
|
17
|
+
};
|
|
18
|
+
export interface CodeGuideProviderProps {
|
|
19
|
+
initialState: {
|
|
20
|
+
codeGuideFiles: CodeGuideFile[];
|
|
21
|
+
highlightedLines: string;
|
|
22
|
+
activeCodeTab: string;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export declare const CodeGuideProvider: ({ children, initialState, }: React.PropsWithChildren<CodeGuideProviderProps>) => React.JSX.Element;
|
|
26
|
+
export declare function useCodeGuideHighlightLines(highlightedLines: string): void;
|
|
27
|
+
type CodeStepHandlersProps = {
|
|
28
|
+
nodeRef: React.RefObject<HTMLDivElement>;
|
|
29
|
+
bindTo: string;
|
|
30
|
+
context: typeof CodeGuideContext.Provider.prototype.value;
|
|
31
|
+
};
|
|
32
|
+
export declare function useCodeStepHandlers({ nodeRef, bindTo, context }: CodeStepHandlersProps): {
|
|
33
|
+
onScroll: (codeBlock: HTMLPreElement | undefined) => void;
|
|
34
|
+
activateSection: (file: string, lines: string, codeBlock: HTMLPreElement | undefined) => void;
|
|
35
|
+
};
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.useCodeStepHandlers = exports.useCodeGuideHighlightLines = exports.CodeGuideProvider = exports.CodeGuideContext = void 0;
|
|
7
|
+
const react_1 = __importDefault(require("react"));
|
|
8
|
+
exports.CodeGuideContext = react_1.default.createContext({
|
|
9
|
+
highlightedLines: '',
|
|
10
|
+
activeCodeTab: '',
|
|
11
|
+
activeDocsSection: '',
|
|
12
|
+
codeBlock: undefined,
|
|
13
|
+
codeGuideFiles: [],
|
|
14
|
+
updateHighlightedLines: (_lines) => { },
|
|
15
|
+
setCurrentCodeSample: (_fileName) => { },
|
|
16
|
+
setActiveDocumentationSection: (_sectionId) => { },
|
|
17
|
+
setCodeNode: (_node) => { },
|
|
18
|
+
});
|
|
19
|
+
const CodeGuideProvider = ({ children, initialState, }) => {
|
|
20
|
+
const contextValue = {
|
|
21
|
+
highlightedLines: initialState.highlightedLines,
|
|
22
|
+
activeCodeTab: initialState.activeCodeTab,
|
|
23
|
+
activeDocsSection: '',
|
|
24
|
+
codeBlock: undefined,
|
|
25
|
+
codeGuideFiles: [],
|
|
26
|
+
updateHighlightedLines: (_lines) => { },
|
|
27
|
+
setCurrentCodeSample: (_fileName) => { },
|
|
28
|
+
setActiveDocumentationSection: (_sectionId) => { },
|
|
29
|
+
setCodeNode: (_node) => { },
|
|
30
|
+
};
|
|
31
|
+
return react_1.default.createElement(exports.CodeGuideContext.Provider, { value: contextValue }, children);
|
|
32
|
+
};
|
|
33
|
+
exports.CodeGuideProvider = CodeGuideProvider;
|
|
34
|
+
// @ts-ignore
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
36
|
+
function useCodeGuideHighlightLines(highlightedLines) { }
|
|
37
|
+
exports.useCodeGuideHighlightLines = useCodeGuideHighlightLines;
|
|
38
|
+
// @ts-ignore
|
|
39
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
40
|
+
function useCodeStepHandlers({ nodeRef, bindTo, context }) {
|
|
41
|
+
const onScroll = (codeBlock) => {
|
|
42
|
+
if (nodeRef.current) {
|
|
43
|
+
const offsetTop = nodeRef.current.getBoundingClientRect().top;
|
|
44
|
+
if (offsetTop < 300 && offsetTop > 150) {
|
|
45
|
+
activateSection('', '', codeBlock);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
// @ts-ignore
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
51
|
+
const activateSection = (file, lines, codeBlock) => {
|
|
52
|
+
codeBlock === null || codeBlock === void 0 ? void 0 : codeBlock.scrollTo(0, 0);
|
|
53
|
+
};
|
|
54
|
+
return {
|
|
55
|
+
onScroll,
|
|
56
|
+
activateSection,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
exports.useCodeStepHandlers = useCodeStepHandlers;
|
|
60
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@redocly/theme-experimental",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Experimental UI components lib",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"theme-experimental",
|
|
7
|
+
"redocly"
|
|
8
|
+
],
|
|
9
|
+
"author": "team@redocly.com",
|
|
10
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
11
|
+
"main": "lib/index.js",
|
|
12
|
+
"types": "lib/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
"./package.json": "./package.json",
|
|
15
|
+
".": "./lib/index.js",
|
|
16
|
+
"./src/": "./src/"
|
|
17
|
+
},
|
|
18
|
+
"peerDependencies": {
|
|
19
|
+
"lodash.throttle": "^4.1.1",
|
|
20
|
+
"prismjs": "^1.28.0",
|
|
21
|
+
"react": "^17.0.0 || ^18.0.0",
|
|
22
|
+
"react-dom": "^17.0.0 || ^18.0.0",
|
|
23
|
+
"react-router-dom": "^6.10.0",
|
|
24
|
+
"styled-components": "^4.1.1 || ^5.3.11",
|
|
25
|
+
"styled-system": "^5.1.5",
|
|
26
|
+
"@markdoc/markdoc": "0.4.0"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@storybook/addon-actions": "7.6.4",
|
|
30
|
+
"@storybook/addon-essentials": "7.6.4",
|
|
31
|
+
"@storybook/addon-interactions": "7.6.4",
|
|
32
|
+
"@storybook/addon-links": "7.6.4",
|
|
33
|
+
"@storybook/addon-viewport": "7.6.4",
|
|
34
|
+
"@storybook/addons": "7.6.4",
|
|
35
|
+
"@storybook/core-common": "7.6.4",
|
|
36
|
+
"@storybook/node-logger": "7.6.4",
|
|
37
|
+
"@storybook/react": "7.6.4",
|
|
38
|
+
"@storybook/react-webpack5": "7.6.4",
|
|
39
|
+
"@storybook/testing-library": "0.2.2",
|
|
40
|
+
"@storybook/theming": "7.6.4",
|
|
41
|
+
"@testing-library/jest-dom": "6.1.5",
|
|
42
|
+
"@testing-library/react": "14.1.2",
|
|
43
|
+
"@testing-library/user-event": "14.5.1",
|
|
44
|
+
"@types/highlight-words-core": "1.2.3",
|
|
45
|
+
"@types/jest": "29.5.11",
|
|
46
|
+
"@types/jest-when": "3.5.5",
|
|
47
|
+
"@types/lodash.throttle": "4.1.9",
|
|
48
|
+
"@types/node": "18.19.3",
|
|
49
|
+
"@types/prismjs": "1.26.3",
|
|
50
|
+
"@types/react": "18.2.45",
|
|
51
|
+
"@types/react-dom": "18.2.17",
|
|
52
|
+
"@types/styled-components": "5.1.34",
|
|
53
|
+
"@types/styled-system": "5.1.22",
|
|
54
|
+
"@typescript-eslint/eslint-plugin": "5.55.0",
|
|
55
|
+
"@typescript-eslint/parser": "5.55.0",
|
|
56
|
+
"chromatic": "6.17.2",
|
|
57
|
+
"concurrently": "7.6.0",
|
|
58
|
+
"jest": "29.5.0",
|
|
59
|
+
"jest-environment-jsdom": "29.5.0",
|
|
60
|
+
"jest-styled-components": "7.2.0",
|
|
61
|
+
"jest-when": "3.6.0",
|
|
62
|
+
"json-schema-to-ts": "2.7.2",
|
|
63
|
+
"lodash.throttle": "4.1.1",
|
|
64
|
+
"npm-run-all": "4.1.5",
|
|
65
|
+
"react-refresh": "0.14.0",
|
|
66
|
+
"react-router-dom": "6.10.0",
|
|
67
|
+
"storybook": "7.6.4",
|
|
68
|
+
"storybook-addon-pseudo-states": "2.1.2",
|
|
69
|
+
"storybook-design-token": "3.0.0-beta.6",
|
|
70
|
+
"styled-components": "5.3.11",
|
|
71
|
+
"styled-system": "5.1.5",
|
|
72
|
+
"ts-jest": "29.1.1",
|
|
73
|
+
"ts-node": "10.9.1",
|
|
74
|
+
"ts-node-dev": "2.0.0",
|
|
75
|
+
"tsc-alias": "1.8.3",
|
|
76
|
+
"tsconfig-paths": "4.2.0",
|
|
77
|
+
"tsconfig-paths-webpack-plugin": "3.5.2",
|
|
78
|
+
"typescript": "5.2.2",
|
|
79
|
+
"webpack": "5.88.2",
|
|
80
|
+
"@redocly/portal-types": "1.2.1"
|
|
81
|
+
},
|
|
82
|
+
"dependencies": {
|
|
83
|
+
"@redocly/theme": "^0.32.2"
|
|
84
|
+
},
|
|
85
|
+
"scripts": {
|
|
86
|
+
"start": "npm-run-all --parallel storybook storybook:tokens:watch",
|
|
87
|
+
"watch": "tsc -p tsconfig.build.json && (concurrently \"tsc -w -p tsconfig.build.json\" \"tsc-alias -w -p tsconfig.build.json\")",
|
|
88
|
+
"ts:check": "tsc --noEmit --skipLibCheck",
|
|
89
|
+
"clean": "rm -rf lib",
|
|
90
|
+
"compile": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",
|
|
91
|
+
"build": "npm run clean && npm run compile",
|
|
92
|
+
"storybook": "storybook dev -p 6007",
|
|
93
|
+
"storybook:build": "npm run storybook:tokens && storybook build",
|
|
94
|
+
"storybook:tokens": "ts-node scripts/generate-css-tokens.ts",
|
|
95
|
+
"storybook:tokens:watch": "ts-node-dev --respawn scripts/generate-css-tokens.ts"
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import styled from 'styled-components';
|
|
3
|
+
|
|
4
|
+
import { CodeBlock } from '@theme/components/CodeBlock/CodeBlock';
|
|
5
|
+
import { CodeGuideContext, useCodeGuideHighlightLines } from '@portal/CodeGuide';
|
|
6
|
+
|
|
7
|
+
export function CodeDemo() {
|
|
8
|
+
const { highlightedLines, activeCodeTab, setCurrentCodeSample, setCodeNode, codeGuideFiles } =
|
|
9
|
+
React.useContext(CodeGuideContext);
|
|
10
|
+
|
|
11
|
+
useCodeGuideHighlightLines(highlightedLines);
|
|
12
|
+
|
|
13
|
+
const codeBlockRef = React.useRef<HTMLPreElement | null>(null);
|
|
14
|
+
|
|
15
|
+
const setRef = (instance?: HTMLPreElement | null) => {
|
|
16
|
+
if (instance) {
|
|
17
|
+
codeBlockRef.current = instance;
|
|
18
|
+
setCodeNode(codeBlockRef);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const activeCodeExample = codeGuideFiles.find(({ name }) => activeCodeTab === name);
|
|
23
|
+
|
|
24
|
+
return (
|
|
25
|
+
<CodeDemoWrapper>
|
|
26
|
+
<CodeBlock
|
|
27
|
+
source={activeCodeExample?.content}
|
|
28
|
+
lang={activeCodeExample?.lang}
|
|
29
|
+
withLineNumbers
|
|
30
|
+
header={{
|
|
31
|
+
controls: {
|
|
32
|
+
copy: {},
|
|
33
|
+
},
|
|
34
|
+
}}
|
|
35
|
+
codeBlockRef={setRef}
|
|
36
|
+
codeBlockMaxHeight="75vh"
|
|
37
|
+
hideCodeColors={true}
|
|
38
|
+
tabs={{
|
|
39
|
+
files: codeGuideFiles,
|
|
40
|
+
activeTabName: activeCodeTab,
|
|
41
|
+
handleTabSwitch: setCurrentCodeSample,
|
|
42
|
+
}}
|
|
43
|
+
/>
|
|
44
|
+
</CodeDemoWrapper>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const CodeDemoWrapper = styled.div`
|
|
49
|
+
background-color: var(--samples-panel-controls-background-color);
|
|
50
|
+
border-radius: 0 var(--global-border-radius);
|
|
51
|
+
padding: 32px 0;
|
|
52
|
+
min-height: 85vh;
|
|
53
|
+
`;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import styled from 'styled-components';
|
|
3
|
+
import { CodeDemo } from '@theme-experimental/layouts/CodeGuide/CodeDemo';
|
|
4
|
+
|
|
5
|
+
import { MarkdownWrapper } from '@theme/components/Markdown/MarkdownWrapper';
|
|
6
|
+
import { CodeGuideProvider } from '@portal/CodeGuide';
|
|
7
|
+
|
|
8
|
+
export default function CodeGuide(props: React.PropsWithChildren<any>) {
|
|
9
|
+
const {
|
|
10
|
+
pageProps: { codeGuideFiles },
|
|
11
|
+
children,
|
|
12
|
+
} = props;
|
|
13
|
+
|
|
14
|
+
const initialState = {
|
|
15
|
+
codeGuideFiles,
|
|
16
|
+
highlightedLines: '',
|
|
17
|
+
activeCodeTab: codeGuideFiles?.[0]?.name || '',
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
return (
|
|
21
|
+
<CodeGuideProvider initialState={initialState}>
|
|
22
|
+
<Wrapper data-component-name="layouts/CodeGuide">
|
|
23
|
+
<MarkdownWrapper>{children}</MarkdownWrapper>
|
|
24
|
+
<div>
|
|
25
|
+
<StickyPosition>
|
|
26
|
+
<CodeDemo />
|
|
27
|
+
</StickyPosition>
|
|
28
|
+
</div>
|
|
29
|
+
</Wrapper>
|
|
30
|
+
</CodeGuideProvider>
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const Wrapper = styled.div`
|
|
35
|
+
padding-left: 24px;
|
|
36
|
+
display: grid;
|
|
37
|
+
grid-template-columns: 40% 55%;
|
|
38
|
+
grid-column-gap: 45px;
|
|
39
|
+
height: 100%;
|
|
40
|
+
`;
|
|
41
|
+
|
|
42
|
+
const StickyPosition = styled.div`
|
|
43
|
+
position: sticky;
|
|
44
|
+
top: 50px;
|
|
45
|
+
left: 0;
|
|
46
|
+
`;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
export const CodeGuideContext = React.createContext({
|
|
4
|
+
highlightedLines: '',
|
|
5
|
+
activeCodeTab: '',
|
|
6
|
+
activeDocsSection: '',
|
|
7
|
+
codeBlock: undefined,
|
|
8
|
+
codeGuideFiles: [] as CodeGuideFile[],
|
|
9
|
+
updateHighlightedLines: (_lines: string) => {},
|
|
10
|
+
setCurrentCodeSample: (_fileName: string) => {},
|
|
11
|
+
setActiveDocumentationSection: (_sectionId: string) => {},
|
|
12
|
+
setCodeNode: (_node: React.MutableRefObject<HTMLPreElement | null>) => {},
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export type CodeGuideFile = { name: string; lang?: string; content: string };
|
|
16
|
+
|
|
17
|
+
export interface CodeGuideProviderProps {
|
|
18
|
+
initialState: {
|
|
19
|
+
codeGuideFiles: CodeGuideFile[];
|
|
20
|
+
highlightedLines: string;
|
|
21
|
+
activeCodeTab: string;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const CodeGuideProvider = ({
|
|
26
|
+
children,
|
|
27
|
+
initialState,
|
|
28
|
+
}: React.PropsWithChildren<CodeGuideProviderProps>) => {
|
|
29
|
+
const contextValue = {
|
|
30
|
+
highlightedLines: initialState.highlightedLines,
|
|
31
|
+
activeCodeTab: initialState.activeCodeTab,
|
|
32
|
+
activeDocsSection: '',
|
|
33
|
+
codeBlock: undefined,
|
|
34
|
+
codeGuideFiles: [] as CodeGuideFile[],
|
|
35
|
+
updateHighlightedLines: (_lines: string) => {},
|
|
36
|
+
setCurrentCodeSample: (_fileName: string) => {},
|
|
37
|
+
setActiveDocumentationSection: (_sectionId: string) => {},
|
|
38
|
+
setCodeNode: (_node: React.MutableRefObject<HTMLPreElement | null>) => {},
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
return <CodeGuideContext.Provider value={contextValue}>{children}</CodeGuideContext.Provider>;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// @ts-ignore
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
46
|
+
export function useCodeGuideHighlightLines(highlightedLines: string) {}
|
|
47
|
+
|
|
48
|
+
type CodeStepHandlersProps = {
|
|
49
|
+
nodeRef: React.RefObject<HTMLDivElement>;
|
|
50
|
+
bindTo: string;
|
|
51
|
+
context: typeof CodeGuideContext.Provider.prototype.value;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// @ts-ignore
|
|
55
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
56
|
+
export function useCodeStepHandlers({ nodeRef, bindTo, context }: CodeStepHandlersProps): {
|
|
57
|
+
onScroll: (codeBlock: HTMLPreElement | undefined) => void;
|
|
58
|
+
activateSection: (file: string, lines: string, codeBlock: HTMLPreElement | undefined) => void;
|
|
59
|
+
} {
|
|
60
|
+
const onScroll = (codeBlock: HTMLPreElement | undefined) => {
|
|
61
|
+
if (nodeRef.current) {
|
|
62
|
+
const offsetTop = nodeRef.current.getBoundingClientRect().top;
|
|
63
|
+
if (offsetTop < 300 && offsetTop > 150) {
|
|
64
|
+
activateSection('', '', codeBlock);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// @ts-ignore
|
|
70
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
71
|
+
const activateSection = (file: string, lines: string, codeBlock: HTMLPreElement | undefined) => {
|
|
72
|
+
codeBlock?.scrollTo(0, 0);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
onScroll,
|
|
77
|
+
activateSection,
|
|
78
|
+
};
|
|
79
|
+
}
|