@redocly/theme 0.47.0 → 0.48.0

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.
Files changed (219) hide show
  1. package/lib/components/Buttons/CopyButton.d.ts +8 -3
  2. package/lib/components/Buttons/CopyButton.js +4 -3
  3. package/lib/components/Feedback/Mood.d.ts +2 -2
  4. package/lib/components/Feedback/Mood.js +6 -6
  5. package/lib/components/Feedback/Rating.d.ts +2 -2
  6. package/lib/components/Feedback/Rating.js +6 -6
  7. package/lib/components/Feedback/Scale.d.ts +2 -2
  8. package/lib/components/Feedback/Scale.js +6 -6
  9. package/lib/components/Feedback/Sentiment.d.ts +2 -2
  10. package/lib/components/Feedback/Sentiment.js +6 -6
  11. package/lib/components/Search/SearchDialog.js +8 -8
  12. package/lib/components/Search/SearchFilter.d.ts +3 -2
  13. package/lib/components/Search/SearchFilter.js +2 -2
  14. package/lib/components/Search/SearchFilterField.d.ts +3 -2
  15. package/lib/components/Search/SearchFilterField.js +2 -2
  16. package/lib/components/Search/SearchGroups.d.ts +5 -4
  17. package/lib/components/Search/SearchGroups.js +3 -3
  18. package/lib/components/StatusCode/StatusCode.js +11 -14
  19. package/lib/core/constants/code-walkthrough.d.ts +1 -0
  20. package/lib/core/constants/code-walkthrough.js +5 -0
  21. package/lib/core/constants/index.d.ts +1 -0
  22. package/lib/core/constants/index.js +1 -0
  23. package/lib/core/contexts/CodeWalkthrough/CodeWalkthroughControlsContext.d.ts +2 -0
  24. package/lib/core/contexts/CodeWalkthrough/CodeWalkthroughControlsContext.js +18 -0
  25. package/lib/core/contexts/CodeWalkthrough/CodeWalkthroughStepsContext.d.ts +2 -0
  26. package/lib/core/contexts/CodeWalkthrough/CodeWalkthroughStepsContext.js +11 -0
  27. package/lib/core/contexts/index.d.ts +2 -0
  28. package/lib/core/contexts/index.js +2 -0
  29. package/lib/core/hooks/__mocks__/search/use-search-filter.d.ts +1 -1
  30. package/lib/core/hooks/__mocks__/search/use-search-filter.js +1 -1
  31. package/lib/core/hooks/code-walkthrough/__mocks__/MockIntersectionObserver.d.ts +10 -0
  32. package/lib/core/hooks/code-walkthrough/__mocks__/MockIntersectionObserver.js +55 -0
  33. package/lib/core/hooks/code-walkthrough/use-code-panel.d.ts +6 -0
  34. package/lib/core/hooks/code-walkthrough/use-code-panel.js +93 -0
  35. package/lib/core/hooks/code-walkthrough/use-code-walkthrough-controls.d.ts +22 -0
  36. package/lib/core/hooks/code-walkthrough/use-code-walkthrough-controls.js +183 -0
  37. package/lib/core/hooks/code-walkthrough/use-code-walkthrough-steps.d.ts +16 -0
  38. package/lib/core/hooks/code-walkthrough/use-code-walkthrough-steps.js +117 -0
  39. package/lib/core/hooks/code-walkthrough/use-code-walkthrough.d.ts +14 -0
  40. package/lib/core/hooks/code-walkthrough/use-code-walkthrough.js +22 -0
  41. package/lib/core/hooks/index.d.ts +4 -0
  42. package/lib/core/hooks/index.js +4 -0
  43. package/lib/core/hooks/search/use-search-filter.d.ts +2 -2
  44. package/lib/core/hooks/search/use-search-filter.js +5 -5
  45. package/lib/core/styles/global.js +2 -0
  46. package/lib/core/templates/Markdown.d.ts +8 -2
  47. package/lib/core/templates/Markdown.js +5 -2
  48. package/lib/core/types/hooks.d.ts +3 -0
  49. package/lib/core/types/l10n.d.ts +1 -1
  50. package/lib/core/types/search.d.ts +1 -2
  51. package/lib/core/utils/download-code-walkthrough.d.ts +4 -0
  52. package/lib/core/utils/download-code-walkthrough.js +32 -0
  53. package/lib/core/utils/get-code-walkthrough-file-text.d.ts +4 -0
  54. package/lib/core/utils/get-code-walkthrough-file-text.js +22 -0
  55. package/lib/core/utils/get-file-icon.d.ts +2 -0
  56. package/lib/core/utils/get-file-icon.js +31 -0
  57. package/lib/core/utils/index.d.ts +5 -0
  58. package/lib/core/utils/index.js +5 -0
  59. package/lib/core/utils/js-utils.d.ts +30 -0
  60. package/lib/core/utils/js-utils.js +41 -0
  61. package/lib/core/utils/match-code-walkthrough-conditions.d.ts +4 -0
  62. package/lib/core/utils/match-code-walkthrough-conditions.js +23 -0
  63. package/lib/core/utils/replace-inputs-with-value.d.ts +3 -0
  64. package/lib/core/utils/replace-inputs-with-value.js +16 -0
  65. package/lib/icons/DocumentCssIcon/DocumentCssIcon.d.ts +9 -0
  66. package/lib/icons/DocumentCssIcon/DocumentCssIcon.js +23 -0
  67. package/lib/icons/DocumentCssIcon/index.d.ts +1 -0
  68. package/lib/icons/DocumentCssIcon/index.js +6 -0
  69. package/lib/icons/DocumentGraphqlIcon/DocumentGraphqlIcon.d.ts +9 -0
  70. package/lib/icons/DocumentGraphqlIcon/DocumentGraphqlIcon.js +31 -0
  71. package/lib/icons/DocumentGraphqlIcon/index.d.ts +1 -0
  72. package/lib/icons/DocumentGraphqlIcon/index.js +6 -0
  73. package/lib/icons/DocumentHtmlIcon/DocumentHtmlIcon.d.ts +9 -0
  74. package/lib/icons/DocumentHtmlIcon/DocumentHtmlIcon.js +23 -0
  75. package/lib/icons/DocumentHtmlIcon/index.d.ts +1 -0
  76. package/lib/icons/DocumentHtmlIcon/index.js +6 -0
  77. package/lib/icons/DocumentJsIcon/DocumentJsIcon.d.ts +9 -0
  78. package/lib/icons/DocumentJsIcon/DocumentJsIcon.js +23 -0
  79. package/lib/icons/DocumentJsIcon/index.d.ts +1 -0
  80. package/lib/icons/DocumentJsIcon/index.js +6 -0
  81. package/lib/icons/DocumentJsonIcon/DocumentJsonIcon.d.ts +9 -0
  82. package/lib/icons/DocumentJsonIcon/DocumentJsonIcon.js +23 -0
  83. package/lib/icons/DocumentJsonIcon/index.d.ts +1 -0
  84. package/lib/icons/DocumentJsonIcon/index.js +6 -0
  85. package/lib/icons/DocumentMarkdownIcon/DocumentMarkdownIcon.d.ts +9 -0
  86. package/lib/icons/DocumentMarkdownIcon/DocumentMarkdownIcon.js +23 -0
  87. package/lib/icons/DocumentMarkdownIcon/index.d.ts +1 -0
  88. package/lib/icons/DocumentMarkdownIcon/index.js +6 -0
  89. package/lib/icons/DocumentReactIcon/DocumentReactIcon.d.ts +9 -0
  90. package/lib/icons/DocumentReactIcon/DocumentReactIcon.js +23 -0
  91. package/lib/icons/DocumentReactIcon/index.d.ts +1 -0
  92. package/lib/icons/DocumentReactIcon/index.js +6 -0
  93. package/lib/icons/DocumentTsIcon/DocumentTsIcon.d.ts +9 -0
  94. package/lib/icons/DocumentTsIcon/DocumentTsIcon.js +23 -0
  95. package/lib/icons/DocumentTsIcon/index.d.ts +1 -0
  96. package/lib/icons/DocumentTsIcon/index.js +6 -0
  97. package/lib/icons/DocumentYamlIcon/DocumentYamlIcon.d.ts +9 -0
  98. package/lib/icons/DocumentYamlIcon/DocumentYamlIcon.js +23 -0
  99. package/lib/icons/DocumentYamlIcon/index.d.ts +1 -0
  100. package/lib/icons/DocumentYamlIcon/index.js +6 -0
  101. package/lib/index.d.ts +1 -0
  102. package/lib/index.js +1 -0
  103. package/lib/layouts/CodeWalkthroughLayout.d.ts +5 -0
  104. package/lib/layouts/CodeWalkthroughLayout.js +72 -0
  105. package/lib/markdoc/attributes/code-walkthrough-filesets.d.ts +5 -0
  106. package/lib/markdoc/attributes/code-walkthrough-filesets.js +16 -0
  107. package/lib/markdoc/attributes/code-walkthrough-filters.d.ts +5 -0
  108. package/lib/markdoc/attributes/code-walkthrough-filters.js +16 -0
  109. package/lib/markdoc/components/CodeWalkthrough/CodeContainer.d.ts +5 -0
  110. package/lib/markdoc/components/CodeWalkthrough/CodeContainer.js +81 -0
  111. package/lib/markdoc/components/CodeWalkthrough/CodeFilters.d.ts +12 -0
  112. package/lib/markdoc/components/CodeWalkthrough/CodeFilters.js +58 -0
  113. package/lib/markdoc/components/CodeWalkthrough/CodePanel.d.ts +8 -0
  114. package/lib/markdoc/components/CodeWalkthrough/CodePanel.js +64 -0
  115. package/lib/markdoc/components/CodeWalkthrough/CodePanelHeader.d.ts +8 -0
  116. package/lib/markdoc/components/CodeWalkthrough/CodePanelHeader.js +155 -0
  117. package/lib/markdoc/components/CodeWalkthrough/CodePanelPreview.d.ts +2 -0
  118. package/lib/markdoc/components/CodeWalkthrough/CodePanelPreview.js +73 -0
  119. package/lib/markdoc/components/CodeWalkthrough/CodePanelToolbar.d.ts +5 -0
  120. package/lib/markdoc/components/CodeWalkthrough/CodePanelToolbar.js +47 -0
  121. package/lib/markdoc/components/CodeWalkthrough/CodeStep.d.ts +12 -0
  122. package/lib/markdoc/components/CodeWalkthrough/CodeStep.js +128 -0
  123. package/lib/markdoc/components/CodeWalkthrough/CodeToggle.d.ts +9 -0
  124. package/lib/markdoc/components/CodeWalkthrough/CodeToggle.js +69 -0
  125. package/lib/markdoc/components/CodeWalkthrough/CodeWalkthrough.d.ts +4 -0
  126. package/lib/markdoc/components/CodeWalkthrough/CodeWalkthrough.js +121 -0
  127. package/lib/markdoc/components/CodeWalkthrough/Input.d.ts +8 -0
  128. package/lib/markdoc/components/CodeWalkthrough/Input.js +99 -0
  129. package/lib/markdoc/components/CodeWalkthrough/variables.d.ts +1 -0
  130. package/lib/markdoc/components/CodeWalkthrough/variables.js +19 -0
  131. package/lib/markdoc/components/default.d.ts +5 -0
  132. package/lib/markdoc/components/default.js +5 -0
  133. package/lib/markdoc/default.js +8 -0
  134. package/lib/markdoc/tags/code-step.d.ts +2 -0
  135. package/lib/markdoc/tags/code-step.js +28 -0
  136. package/lib/markdoc/tags/code-toggle.d.ts +2 -0
  137. package/lib/markdoc/tags/code-toggle.js +40 -0
  138. package/lib/markdoc/tags/code-walkthrough.d.ts +8 -0
  139. package/lib/markdoc/tags/code-walkthrough.js +242 -0
  140. package/lib/markdoc/tags/input.d.ts +2 -0
  141. package/lib/markdoc/tags/input.js +37 -0
  142. package/lib/plugin.d.ts +2 -1
  143. package/lib/plugin.js +2 -4
  144. package/package.json +10 -3
  145. package/src/components/Buttons/CopyButton.tsx +24 -6
  146. package/src/components/Feedback/Mood.tsx +9 -10
  147. package/src/components/Feedback/Rating.tsx +9 -10
  148. package/src/components/Feedback/Scale.tsx +9 -10
  149. package/src/components/Feedback/Sentiment.tsx +9 -10
  150. package/src/components/Search/SearchDialog.tsx +11 -6
  151. package/src/components/Search/SearchFilter.tsx +6 -3
  152. package/src/components/Search/SearchFilterField.tsx +4 -2
  153. package/src/components/Search/SearchGroups.tsx +13 -8
  154. package/src/components/StatusCode/StatusCode.ts +11 -10
  155. package/src/core/constants/code-walkthrough.ts +1 -0
  156. package/src/core/constants/index.ts +1 -0
  157. package/src/core/contexts/CodeWalkthrough/CodeWalkthroughControlsContext.tsx +21 -0
  158. package/src/core/contexts/CodeWalkthrough/CodeWalkthroughStepsContext.tsx +10 -0
  159. package/src/core/contexts/index.ts +2 -0
  160. package/src/core/hooks/__mocks__/search/use-search-filter.ts +1 -1
  161. package/src/core/hooks/code-walkthrough/__mocks__/MockIntersectionObserver.ts +22 -0
  162. package/src/core/hooks/code-walkthrough/use-code-panel.ts +164 -0
  163. package/src/core/hooks/code-walkthrough/use-code-walkthrough-controls.ts +302 -0
  164. package/src/core/hooks/code-walkthrough/use-code-walkthrough-steps.ts +165 -0
  165. package/src/core/hooks/code-walkthrough/use-code-walkthrough.ts +51 -0
  166. package/src/core/hooks/index.ts +4 -0
  167. package/src/core/hooks/search/use-search-filter.ts +9 -5
  168. package/src/core/styles/global.ts +2 -0
  169. package/src/core/templates/Markdown.tsx +16 -5
  170. package/src/core/types/hooks.ts +3 -0
  171. package/src/core/types/l10n.ts +5 -3
  172. package/src/core/types/search.ts +1 -2
  173. package/src/core/utils/download-code-walkthrough.ts +25 -0
  174. package/src/core/utils/get-code-walkthrough-file-text.ts +32 -0
  175. package/src/core/utils/get-file-icon.ts +35 -0
  176. package/src/core/utils/index.ts +5 -0
  177. package/src/core/utils/js-utils.ts +48 -0
  178. package/src/core/utils/match-code-walkthrough-conditions.ts +29 -0
  179. package/src/core/utils/replace-inputs-with-value.ts +11 -0
  180. package/src/icons/DocumentCssIcon/DocumentCssIcon.tsx +36 -0
  181. package/src/icons/DocumentCssIcon/index.ts +1 -0
  182. package/src/icons/DocumentGraphqlIcon/DocumentGraphqlIcon.tsx +64 -0
  183. package/src/icons/DocumentGraphqlIcon/index.ts +1 -0
  184. package/src/icons/DocumentHtmlIcon/DocumentHtmlIcon.tsx +36 -0
  185. package/src/icons/DocumentHtmlIcon/index.ts +1 -0
  186. package/src/icons/DocumentJsIcon/DocumentJsIcon.tsx +36 -0
  187. package/src/icons/DocumentJsIcon/index.ts +1 -0
  188. package/src/icons/DocumentJsonIcon/DocumentJsonIcon.tsx +36 -0
  189. package/src/icons/DocumentJsonIcon/index.ts +1 -0
  190. package/src/icons/DocumentMarkdownIcon/DocumentMarkdownIcon.tsx +36 -0
  191. package/src/icons/DocumentMarkdownIcon/index.ts +1 -0
  192. package/src/icons/DocumentReactIcon/DocumentReactIcon.tsx +36 -0
  193. package/src/icons/DocumentReactIcon/index.ts +1 -0
  194. package/src/icons/DocumentTsIcon/DocumentTsIcon.tsx +38 -0
  195. package/src/icons/DocumentTsIcon/index.ts +1 -0
  196. package/src/icons/DocumentYamlIcon/DocumentYamlIcon.tsx +36 -0
  197. package/src/icons/DocumentYamlIcon/index.ts +1 -0
  198. package/src/index.ts +1 -0
  199. package/src/layouts/CodeWalkthroughLayout.tsx +78 -0
  200. package/src/markdoc/attributes/code-walkthrough-filesets.ts +9 -0
  201. package/src/markdoc/attributes/code-walkthrough-filters.ts +9 -0
  202. package/src/markdoc/components/CodeWalkthrough/CodeContainer.tsx +76 -0
  203. package/src/markdoc/components/CodeWalkthrough/CodeFilters.tsx +87 -0
  204. package/src/markdoc/components/CodeWalkthrough/CodePanel.tsx +68 -0
  205. package/src/markdoc/components/CodeWalkthrough/CodePanelHeader.tsx +192 -0
  206. package/src/markdoc/components/CodeWalkthrough/CodePanelPreview.tsx +53 -0
  207. package/src/markdoc/components/CodeWalkthrough/CodePanelToolbar.tsx +31 -0
  208. package/src/markdoc/components/CodeWalkthrough/CodeStep.tsx +149 -0
  209. package/src/markdoc/components/CodeWalkthrough/CodeToggle.tsx +63 -0
  210. package/src/markdoc/components/CodeWalkthrough/CodeWalkthrough.tsx +111 -0
  211. package/src/markdoc/components/CodeWalkthrough/Input.tsx +101 -0
  212. package/src/markdoc/components/CodeWalkthrough/variables.ts +16 -0
  213. package/src/markdoc/components/default.ts +5 -0
  214. package/src/markdoc/default.ts +8 -0
  215. package/src/markdoc/tags/code-step.ts +27 -0
  216. package/src/markdoc/tags/code-toggle.ts +37 -0
  217. package/src/markdoc/tags/code-walkthrough.ts +315 -0
  218. package/src/markdoc/tags/input.ts +35 -0
  219. package/src/plugin.js +2 -2
@@ -0,0 +1,53 @@
1
+ import React, { PropsWithChildren, useState } from 'react';
2
+ import styled from 'styled-components';
3
+
4
+ import { useThemeHooks } from '@redocly/theme/core/hooks';
5
+ import { ChevronUpIcon } from '@redocly/theme/icons/ChevronUpIcon/ChevronUpIcon';
6
+ import { ChevronDownIcon } from '@redocly/theme/icons/ChevronDownIcon/ChevronDownIcon';
7
+
8
+ export function CodePanelPreview({ children }: PropsWithChildren): JSX.Element {
9
+ const [isOpen, setIsOpen] = useState(false);
10
+ const { useTranslate } = useThemeHooks();
11
+ const { translate } = useTranslate();
12
+
13
+ return (
14
+ <CodePanelPreviewWrapper>
15
+ <PreviewDropdown onClick={() => setIsOpen(!isOpen)}>
16
+ {translate('codeWalkthrough.preview', 'Preview')}
17
+ {isOpen ? <ChevronUpIcon /> : <ChevronDownIcon />}
18
+ </PreviewDropdown>
19
+ <PreviewContentWrapper isOpen={isOpen}>{children}</PreviewContentWrapper>
20
+ </CodePanelPreviewWrapper>
21
+ );
22
+ }
23
+
24
+ const CodePanelPreviewWrapper = styled.div`
25
+ width: 100%;
26
+ padding: var(--spacing-xs) var(--spacing-xs) var(--spacing-xs) var(--spacing-sm);
27
+ border-bottom: 1px solid var(--border-color-secondary);
28
+ `;
29
+
30
+ const PreviewDropdown = styled.div`
31
+ display: flex;
32
+ align-items: center;
33
+
34
+ gap: (--spacing-xxs);
35
+
36
+ user-select: none;
37
+
38
+ font-size: var(--font-size-base);
39
+ cursor: pointer;
40
+ `;
41
+
42
+ const PreviewContentWrapper = styled.div<{ isOpen: boolean }>`
43
+ display: ${({ isOpen }) => (isOpen ? 'grid' : 'none')};
44
+ place-items: center;
45
+
46
+ margin: auto 0;
47
+
48
+ max-height: 50vh;
49
+ width: 100%;
50
+ max-width: 100%;
51
+
52
+ overflow: auto;
53
+ `;
@@ -0,0 +1,31 @@
1
+ import React, { useContext } from 'react';
2
+ import styled from 'styled-components';
3
+
4
+ import type { CodeWalkthroughFile } from '@redocly/config';
5
+
6
+ import { CodeWalkthroughControlsStateContext } from '@redocly/theme/core/contexts';
7
+ import { CopyButton } from '@redocly/theme/components/Buttons/CopyButton';
8
+
9
+ export type CodeToolbarProps = {
10
+ file: CodeWalkthroughFile;
11
+ };
12
+
13
+ export function CodePanelToolbar({ file }: CodeToolbarProps): JSX.Element {
14
+ const { getFileText } = useContext(CodeWalkthroughControlsStateContext);
15
+
16
+ const fileContent = getFileText(file);
17
+
18
+ return (
19
+ <CodeToolbarWrapper>
20
+ <CopyButton data={fileContent} type="compound" variant="secondary" size="medium" />
21
+ </CodeToolbarWrapper>
22
+ );
23
+ }
24
+
25
+ const CodeToolbarWrapper = styled.div`
26
+ display: flex;
27
+ position: absolute;
28
+ bottom: var(--spacing-sm);
29
+ right: var(--spacing-sm);
30
+ gap: var(--spacing-xs);
31
+ `;
@@ -0,0 +1,149 @@
1
+ import React, { useContext, useRef, type PropsWithChildren, useEffect, useState } from 'react';
2
+ import styled from 'styled-components';
3
+
4
+ import type { WithConditions } from '@redocly/config';
5
+
6
+ import {
7
+ CodeWalkthroughControlsStateContext,
8
+ CodeWalkthroughStepsContext,
9
+ } from '@redocly/theme/core/contexts';
10
+
11
+ export type CodeStepProps = WithConditions<{
12
+ id: string;
13
+ stepKey: number;
14
+ heading?: string;
15
+ }>;
16
+
17
+ export function CodeStep({
18
+ id,
19
+ heading,
20
+ stepKey,
21
+ when,
22
+ unless,
23
+ children,
24
+ }: PropsWithChildren<CodeStepProps>) {
25
+ const compRef = useRef<HTMLDivElement | null>(null);
26
+ const { areConditionsMet } = useContext(CodeWalkthroughControlsStateContext);
27
+ const { activeStep, setActiveStep, register, unregister, lockObserver, filtersElementRef } =
28
+ useContext(CodeWalkthroughStepsContext);
29
+ const isActive = activeStep === id;
30
+
31
+ const [scrollMarginTop, setScrollMarginTop] = useState(0);
32
+
33
+ const handleActivateStep = () => {
34
+ if (lockObserver) {
35
+ lockObserver.current = true;
36
+
37
+ if (compRef.current) {
38
+ compRef.current.scrollIntoView({ behavior: 'smooth', block: 'start' });
39
+ }
40
+
41
+ setActiveStep(id);
42
+ setTimeout(() => {
43
+ lockObserver.current = false;
44
+ }, 1000);
45
+ }
46
+ };
47
+
48
+ useEffect(() => {
49
+ // If the step is active during first render, scroll to it
50
+ // This is to ensure that the step is visible when the page is loaded
51
+ if (isActive) {
52
+ handleActivateStep();
53
+ }
54
+
55
+ // Ignore dependency array because we only need to run this once
56
+ // eslint-disable-next-line react-hooks/exhaustive-deps
57
+ }, []);
58
+
59
+ useEffect(() => {
60
+ const currentCompRef = compRef.current;
61
+ if (currentCompRef) {
62
+ currentCompRef
63
+ .querySelectorAll<HTMLElement>('a, button, input, textarea, select, [tabindex]')
64
+ .forEach((el) => {
65
+ el.setAttribute('tabindex', '-1');
66
+ });
67
+
68
+ register(currentCompRef);
69
+ }
70
+
71
+ const filtersElementHeight = filtersElementRef?.current?.clientHeight || 0;
72
+ const navbarHeight = document.querySelector('nav')?.clientHeight || 0;
73
+ setScrollMarginTop(filtersElementHeight + navbarHeight);
74
+
75
+ return () => {
76
+ if (currentCompRef) {
77
+ unregister(currentCompRef);
78
+ }
79
+ };
80
+ }, [activeStep, register, unregister, filtersElementRef]);
81
+
82
+ if (!areConditionsMet({ when, unless })) {
83
+ return null;
84
+ }
85
+
86
+ return (
87
+ <StepWrapper
88
+ ref={compRef}
89
+ isActive={isActive}
90
+ scrollMarginTop={scrollMarginTop}
91
+ data-step-key={stepKey}
92
+ data-step-active={isActive}
93
+ onClick={handleActivateStep}
94
+ onFocus={handleActivateStep}
95
+ tabIndex={0}
96
+ >
97
+ <StepContent isActive={isActive}>
98
+ {heading ? <StepHeading>{heading}</StepHeading> : null}
99
+ {children}
100
+ </StepContent>
101
+ </StepWrapper>
102
+ );
103
+ }
104
+
105
+ const StepContent = styled.div<{ isActive: boolean }>`
106
+ margin: var(--spacing-xs) 0px var(--spacing-xs) calc(var(--spacing-unit) * 3.5);
107
+ padding: var(--spacing-md) var(--spacing-lg);
108
+ background: ${({ isActive }) => (isActive ? 'var(--layer-color)' : 'none')};
109
+ border-radius: var(--border-radius);
110
+
111
+ &:hover {
112
+ background-color: ${({ isActive }) =>
113
+ isActive ? 'var(--code-step-bg-active-hover)' : 'var(--code-step-bg-hover)'};
114
+ }
115
+ `;
116
+
117
+ const StepHeading = styled.p`
118
+ font-weight: var(--font-weight-semibold);
119
+ `;
120
+
121
+ export const StepWrapper = styled.div<{ isActive: boolean; scrollMarginTop: number }>`
122
+ position: relative;
123
+ scroll-margin-top: ${({ scrollMarginTop }) => scrollMarginTop}px;
124
+
125
+ &::before {
126
+ content: '';
127
+ position: absolute;
128
+ width: 6px;
129
+ height: 100%;
130
+ background-color: ${({ isActive }) =>
131
+ isActive ? 'var(--code-step-vertical-line-bg-active)' : 'none'};
132
+ border-radius: var(--border-radius-lg);
133
+ }
134
+ &:hover::before {
135
+ background-color: ${({ isActive }) =>
136
+ isActive
137
+ ? 'var(--code-step-vertical-line-bg-active)'
138
+ : 'var(--code-step-vertical-line-bg-hover)'};
139
+ }
140
+
141
+ &:hover::before {
142
+ width: ${({ isActive }) => (isActive ? '8px' : '6px')};
143
+ }
144
+
145
+ &:hover ${StepContent} {
146
+ background-color: ${({ isActive }) =>
147
+ isActive ? 'var(--code-step-bg-active-hover)' : 'var(--code-step-bg-hover)'};
148
+ }
149
+ `;
@@ -0,0 +1,63 @@
1
+ import React, { useContext } from 'react';
2
+ import styled from 'styled-components';
3
+
4
+ import { CodeWalkthroughControlsStateContext } from '@redocly/theme/core/contexts';
5
+ import { StepWrapper } from '@redocly/theme/markdoc/components/CodeWalkthrough/CodeStep';
6
+ import { Switch } from '@redocly/theme/components/Switch/Switch';
7
+
8
+ export type CodeToggleProps = React.PropsWithChildren<{
9
+ id: string;
10
+ label: string;
11
+ description?: React.ReactNode[];
12
+ }>;
13
+
14
+ export function CodeToggle(props: CodeToggleProps) {
15
+ const { id, label, description, children } = props;
16
+ const { getToggleState, changeToggleState } = useContext(CodeWalkthroughControlsStateContext);
17
+
18
+ const toggleState = getToggleState(id);
19
+
20
+ if (!(toggleState && toggleState.render)) {
21
+ return null;
22
+ }
23
+ const checked = toggleState.value;
24
+
25
+ return (
26
+ <ToggleWrapper>
27
+ <ToggleContentWrapper>
28
+ <ToggleSubtitle>
29
+ <Switch value={checked} onChange={(newValue) => changeToggleState(id, newValue)} />
30
+ <div>{label}</div>
31
+ </ToggleSubtitle>
32
+ {description ? (
33
+ <div>
34
+ {description.map((paragraph, idx) => (
35
+ <React.Fragment key={idx}>{paragraph}</React.Fragment>
36
+ ))}
37
+ </div>
38
+ ) : null}
39
+ </ToggleContentWrapper>
40
+ {checked ? children : null}
41
+ </ToggleWrapper>
42
+ );
43
+ }
44
+
45
+ export const ToggleWrapper = styled.div`
46
+ ${StepWrapper} {
47
+ margin-left: 0;
48
+ }
49
+ `;
50
+
51
+ export const ToggleSubtitle = styled.div`
52
+ display: flex;
53
+ gap: var(--spacing-sm);
54
+ align-items: center;
55
+ font-weight: var(--font-weight-bold);
56
+ `;
57
+
58
+ const ToggleContentWrapper = styled.div`
59
+ display: flex;
60
+ flex-direction: column;
61
+ gap: var(--spacing-xs);
62
+ padding: var(--spacing-xs) 0;
63
+ `;
@@ -0,0 +1,111 @@
1
+ import React, { useState, type PropsWithChildren } from 'react';
2
+ import styled from 'styled-components';
3
+
4
+ import type { CodeWalkthroughAttr } from '@redocly/config';
5
+
6
+ import {
7
+ CodeWalkthroughStepsContext,
8
+ CodeWalkthroughControlsStateContext,
9
+ } from '@redocly/theme/core/contexts';
10
+ import { CodePanel } from '@redocly/theme/markdoc/components/CodeWalkthrough/CodePanel';
11
+ import { useCodeWalkthrough } from '@redocly/theme/core/hooks';
12
+ import { CodeFilters } from '@redocly/theme/markdoc/components/CodeWalkthrough/CodeFilters';
13
+ import { StepWrapper } from '@redocly/theme/markdoc/components/CodeWalkthrough/CodeStep';
14
+
15
+ export type CodeWalkthroughProps = PropsWithChildren<CodeWalkthroughAttr>;
16
+
17
+ export function CodeWalkthrough({ children, steps, preview, ...attributes }: CodeWalkthroughProps) {
18
+ // Steps are being modified causing the observer to be recreated in the useCodeWalkthrough hook
19
+ // This is a workaround to prevent the observer from being recreated
20
+ const [initialSteps] = useState([...steps]);
21
+ const { controlsState, stepsState, files, downloadAssociatedFiles } = useCodeWalkthrough(
22
+ initialSteps,
23
+ attributes,
24
+ );
25
+ const { activeFilters, getFilterState, changeFilterState } = controlsState;
26
+ const { filtersElementRef } = stepsState;
27
+
28
+ return (
29
+ <CodeWalkthroughStepsContext.Provider value={stepsState}>
30
+ <CodeWalkthroughControlsStateContext.Provider value={controlsState}>
31
+ <CodeWalkthroughWrapper className="code-walkthrough">
32
+ <DocsPanel>
33
+ <CodeFilters
34
+ filters={activeFilters}
35
+ getFilterState={getFilterState}
36
+ handleFilterSelect={changeFilterState}
37
+ filtersElementRef={filtersElementRef}
38
+ />
39
+ <ContentWrapper>{children}</ContentWrapper>
40
+ </DocsPanel>
41
+
42
+ <CodePanel
43
+ files={files}
44
+ downloadAssociatedFiles={downloadAssociatedFiles}
45
+ preview={preview}
46
+ />
47
+ </CodeWalkthroughWrapper>
48
+ </CodeWalkthroughControlsStateContext.Provider>
49
+ </CodeWalkthroughStepsContext.Provider>
50
+ );
51
+ }
52
+
53
+ const CodeWalkthroughWrapper = styled.div`
54
+ display: grid;
55
+ grid-template-columns: 4fr 6fr;
56
+
57
+ padding-right: var(--spacing-xl);
58
+
59
+ border-top: 1px solid var(--border-color-secondary);
60
+ border-bottom: 1px solid var(--border-color-secondary);
61
+
62
+ &:first-child {
63
+ border-top: none;
64
+ }
65
+
66
+ &:last-child {
67
+ border-bottom: none;
68
+ }
69
+ `;
70
+
71
+ const DocsPanel = styled.div`
72
+ display: flex;
73
+ flex-direction: column;
74
+
75
+ min-height: 0;
76
+ min-height: calc(100vh - var(--navbar-height));
77
+
78
+ padding-right: var(--spacing-xs);
79
+ padding-bottom: calc(var(--spacing-xs) + var(--spacing-xl));
80
+ gap: var(--spacing-xl);
81
+ `;
82
+
83
+ const ContentWrapper = styled.div`
84
+ flex-grow: 1;
85
+
86
+ min-height: 0;
87
+ max-width: var(--md-content-max-width);
88
+
89
+ & > *:not(${StepWrapper}) {
90
+ padding-left: var(--spacing-xl);
91
+ }
92
+ overflow-y: scroll;
93
+
94
+ /* Hide scrollbar for Chrome, Safari and Opera */
95
+ &::-webkit-scrollbar {
96
+ display: none;
97
+ }
98
+ /* Hide scrollbar for IE, Edge and Firefox */
99
+ -ms-overflow-style: none; /* IE and Edge */
100
+ scrollbar-width: none; /* Firefox */
101
+
102
+ &:first-child {
103
+ padding-top: var(--spacing-sm);
104
+ }
105
+
106
+ &:not(:first-child) {
107
+ & > *:first-child {
108
+ margin-top: 0;
109
+ }
110
+ }
111
+ `;
@@ -0,0 +1,101 @@
1
+ import React, { useContext, useState, useCallback } from 'react';
2
+ import styled from 'styled-components';
3
+ import debounce from 'lodash.debounce';
4
+
5
+ import { CodeWalkthroughControlsStateContext } from '@redocly/theme/core/contexts';
6
+
7
+ export type InputProps = {
8
+ id: string;
9
+ label?: string;
10
+ placeholder?: string;
11
+ value?: string;
12
+ };
13
+
14
+ const DEBOUNCE_TIME = 500;
15
+
16
+ export function Input(props: InputProps) {
17
+ const { id, label, placeholder } = props;
18
+ const { getInputState, changeInputState } = useContext(CodeWalkthroughControlsStateContext);
19
+
20
+ const inputState = getInputState(id);
21
+
22
+ const [value, setValue] = useState(inputState?.value || '');
23
+
24
+ // eslint-disable-next-line react-hooks/exhaustive-deps
25
+ const debouncedSave = useCallback(
26
+ debounce((id: string, value: string) => {
27
+ changeInputState(id, value);
28
+ }, DEBOUNCE_TIME),
29
+ [],
30
+ );
31
+
32
+ if (!inputState?.render) {
33
+ return null;
34
+ }
35
+
36
+ const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
37
+ const inputValue = event.target.value;
38
+
39
+ setValue(inputValue);
40
+ debouncedSave(id, inputValue);
41
+ };
42
+
43
+ return (
44
+ <InputWrapper>
45
+ {label && <Label>{label}</Label>}
46
+ <StyledInput
47
+ id={id}
48
+ value={value}
49
+ onChange={handleChange}
50
+ aria-label={label}
51
+ placeholder={placeholder}
52
+ />
53
+ </InputWrapper>
54
+ );
55
+ }
56
+
57
+ const InputWrapper = styled.div`
58
+ display: flex;
59
+ flex-direction: column;
60
+ margin: var(--md-paragraph-margin);
61
+ `;
62
+
63
+ const Label = styled.p`
64
+ color: var(--text-color-secondary);
65
+ font-size: var(--font-size-base);
66
+ line-height: var(--line-height-base);
67
+ font-family: var(--font-family-base);
68
+ font-weight: var(--font-weight-medium);
69
+ `;
70
+
71
+ const StyledInput = styled.input`
72
+ border: 1px solid var(--border-color-primary);
73
+ width: 224px;
74
+ outline: none;
75
+ padding: var(--input-padding) var(--spacing-sm);
76
+ border-radius: var(--input-border-radius);
77
+ background-color: var(--input-bg-color);
78
+ color: var(--text-color-secondary);
79
+ font-family: var(--font-family-base);
80
+ font-size: var(--font-size-base);
81
+ line-height: var(--input-line-height);
82
+
83
+ &::placeholder {
84
+ opacity: 1;
85
+ color: var(--input-content-placeholder-color);
86
+ }
87
+
88
+ &:hover {
89
+ color: var(--text-color-primary);
90
+ border: 1px solid var(--border-color-primary);
91
+ }
92
+
93
+ &:focus {
94
+ color: var(--text-color-primary);
95
+ border: 1px solid var(--border-color-primary);
96
+ }
97
+
98
+ &:-webkit-autofill {
99
+ background-color: var(--input-bg-color);
100
+ }
101
+ `;
@@ -0,0 +1,16 @@
1
+ import { css } from 'styled-components';
2
+
3
+ export const codeWalkthrough = css`
4
+ --code-step-vertical-line-bg-active: var(--color-blue-6);
5
+ --code-step-vertical-line-bg-hover: var(--color-hover-base);
6
+
7
+ --code-step-bg-active: var(--layer-color);
8
+ --code-step-bg-hover: var(--color-hover-base);
9
+ --code-step-bg-active-hover: var(--layer-color-hover);
10
+
11
+ --code-panel-bg-color: var(--layer-color);
12
+ --code-panel-line-numbering-color: var(--text-color-disabled);
13
+ --code-panel-header-height: var(--spacing-lg);
14
+ --code-panel-border-color: var(--border-color-secondary);
15
+ --code-panel-border-radius: var(--border-radius);
16
+ `;
@@ -12,3 +12,8 @@ export * from '@redocly/theme/markdoc/components/Cards/Card';
12
12
  export * from '@redocly/theme/markdoc/components/Cards/CardIcon';
13
13
  export * from '@redocly/theme/markdoc/components/Cards/CardImage';
14
14
  export * from '@redocly/theme/markdoc/components/Image/Image';
15
+ export * from '@redocly/theme/markdoc/components/CodeWalkthrough/CodeWalkthrough';
16
+ export * from '@redocly/theme/markdoc/components/CodeWalkthrough/CodeStep';
17
+ export * from '@redocly/theme/markdoc/components/CodeWalkthrough/Input';
18
+ export * from '@redocly/theme/markdoc/components/CodeWalkthrough/CodeToggle';
19
+ export * from '@redocly/theme/markdoc/components/CodeWalkthrough/CodeContainer';
@@ -19,6 +19,10 @@ import { inlineSvg } from '@redocly/theme/markdoc/tags/inline-svg';
19
19
  import { cards } from '@redocly/theme/markdoc/tags/cards';
20
20
  import { card } from '@redocly/theme/markdoc/tags/card';
21
21
  import { img } from '@redocly/theme/markdoc/tags/img';
22
+ import { codeWalkthrough } from '@redocly/theme/markdoc/tags/code-walkthrough';
23
+ import { codeStep } from '@redocly/theme/markdoc/tags/code-step';
24
+ import { input } from '@redocly/theme/markdoc/tags/input';
25
+ import { toggle } from '@redocly/theme/markdoc/tags/code-toggle';
22
26
 
23
27
  export const tags = {
24
28
  [admonition.tagName]: admonition.schema,
@@ -33,4 +37,8 @@ export const tags = {
33
37
  [cards.tagName]: cards.schema,
34
38
  [card.tagName]: card.schema,
35
39
  [img.tagName]: img.schema,
40
+ [codeWalkthrough.tagName]: codeWalkthrough.schema,
41
+ [codeStep.tagName]: codeStep.schema,
42
+ [toggle.tagName]: toggle.schema,
43
+ [input.tagName]: input.schema,
36
44
  };
@@ -0,0 +1,27 @@
1
+ import type { MarkdocSchemaWrapper } from '@redocly/theme/markdoc/tags/types';
2
+
3
+ export const codeStep: MarkdocSchemaWrapper = {
4
+ tagName: 'step',
5
+ schema: {
6
+ attributes: {
7
+ id: {
8
+ type: String,
9
+ required: true,
10
+ },
11
+ heading: {
12
+ type: String,
13
+ },
14
+ when: {
15
+ type: Object,
16
+ },
17
+ unless: {
18
+ type: Object,
19
+ },
20
+ stepKey: {
21
+ type: Number, // internal
22
+ },
23
+ },
24
+
25
+ render: 'CodeStep',
26
+ },
27
+ };
@@ -0,0 +1,37 @@
1
+ import markdoc from '@markdoc/markdoc';
2
+
3
+ import type { MarkdocSchemaWrapper } from '@redocly/theme/markdoc/tags/types';
4
+
5
+ export const toggle: MarkdocSchemaWrapper = {
6
+ tagName: 'toggle',
7
+ schema: {
8
+ attributes: {
9
+ id: {
10
+ type: String,
11
+ required: true,
12
+ },
13
+ label: {
14
+ type: String,
15
+ required: true,
16
+ },
17
+ unless: {
18
+ type: Object,
19
+ required: false,
20
+ },
21
+ when: {
22
+ type: Object,
23
+ required: false,
24
+ },
25
+ },
26
+ slots: {
27
+ description: { required: false, render: true },
28
+ },
29
+ transform: (node, config) => {
30
+ const attributes = node.transformAttributes(config);
31
+ const children = node.transformChildren(config);
32
+
33
+ return new markdoc.Tag('CodeToggle', attributes, children);
34
+ },
35
+ render: 'CodeToggle',
36
+ },
37
+ };