@gitbook/react-openapi 1.0.4 → 1.0.5

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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # @gitbook/react-openapi
2
2
 
3
+ ## 1.0.5
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [53f5dbe]
8
+ - @gitbook/openapi-parser@2.0.2
9
+
3
10
  ## 1.0.4
4
11
 
5
12
  ### Patch Changes
@@ -3,6 +3,7 @@ import clsx from 'clsx';
3
3
  import { useRef, useState } from 'react';
4
4
  import { mergeProps, useButton, useDisclosure, useFocusRing } from 'react-aria';
5
5
  import { useDisclosureState } from 'react-stately';
6
+ import { Section, SectionBody, SectionHeader, SectionHeaderContent } from './StaticSection';
6
7
  /**
7
8
  * To optimize rendering, most of the components are server-components,
8
9
  * and the interactiveness is mainly handled by a few key components like this one.
@@ -20,13 +21,13 @@ export function InteractiveSection(props) {
20
21
  var _k = useDisclosure({}, state, panelRef), triggerProps = _k.buttonProps, panelProps = _k.panelProps;
21
22
  var buttonProps = useButton(triggerProps, triggerRef).buttonProps;
22
23
  var _l = useFocusRing(), isFocusVisible = _l.isFocusVisible, focusProps = _l.focusProps;
23
- return (<div id={id} className={clsx('openapi-section', toggeable ? 'openapi-section-toggeable' : null, className, toggeable ? "".concat(className, "-").concat(state.isExpanded ? 'opened' : 'closed') : null)}>
24
- {header ? (<div onClick={function () {
24
+ return (<Section id={id} className={clsx('openapi-section', toggeable ? 'openapi-section-toggeable' : null, className, toggeable ? "".concat(className, "-").concat(state.isExpanded ? 'opened' : 'closed') : null)}>
25
+ {header ? (<SectionHeader onClick={function () {
25
26
  if (toggeable) {
26
27
  state.toggle();
27
28
  }
28
- }} className={clsx('openapi-section-header', "".concat(className, "-header"))}>
29
- <div className={clsx('openapi-section-header-content', "".concat(className, "-header-content"))}>
29
+ }} className={className}>
30
+ <SectionHeaderContent className={className}>
30
31
  {(children || (selectedTab === null || selectedTab === void 0 ? void 0 : selectedTab.body)) && toggeable ? (<button {...mergeProps(buttonProps, focusProps)} ref={triggerRef} className={clsx('openapi-section-toggle', "".concat(className, "-toggle"))} style={{
31
32
  outline: isFocusVisible
32
33
  ? '2px solid rgb(var(--primary-color-500) / 0.4)'
@@ -35,7 +36,7 @@ export function InteractiveSection(props) {
35
36
  {toggleIcon}
36
37
  </button>) : null}
37
38
  {header}
38
- </div>
39
+ </SectionHeaderContent>
39
40
  <div className={clsx('openapi-section-header-controls', "".concat(className, "-header-controls"))} onClick={function (event) {
40
41
  event.stopPropagation();
41
42
  }}>
@@ -48,13 +49,13 @@ export function InteractiveSection(props) {
48
49
  </option>); })}
49
50
  </select>) : null}
50
51
  </div>
51
- </div>) : null}
52
- {(!toggeable || state.isExpanded) && (children || (selectedTab === null || selectedTab === void 0 ? void 0 : selectedTab.body)) ? (<div ref={panelRef} {...panelProps} className={clsx('openapi-section-body', "".concat(className, "-body"))}>
52
+ </SectionHeader>) : null}
53
+ {(!toggeable || state.isExpanded) && (children || (selectedTab === null || selectedTab === void 0 ? void 0 : selectedTab.body)) ? (<SectionBody ref={panelRef} {...panelProps} className={className}>
53
54
  {children}
54
55
  {selectedTab === null || selectedTab === void 0 ? void 0 : selectedTab.body}
55
- </div>) : null}
56
+ </SectionBody>) : null}
56
57
  {overlay ? (<div className={clsx('openapi-section-overlay', "".concat(className, "-overlay"))}>
57
58
  {overlay}
58
59
  </div>) : null}
59
- </div>);
60
+ </Section>);
60
61
  }
@@ -9,8 +9,8 @@ var __assign = (this && this.__assign) || function () {
9
9
  };
10
10
  return __assign.apply(this, arguments);
11
11
  };
12
- import { InteractiveSection } from './InteractiveSection';
13
12
  import { OpenAPITabs, OpenAPITabsList, OpenAPITabsPanels } from './OpenAPITabs';
13
+ import { StaticSection } from './StaticSection';
14
14
  import { codeSampleGenerators } from './code-samples';
15
15
  import { generateMediaTypeExample, generateSchemaExample } from './generateSchemaExample';
16
16
  import { stringifyOpenAPI } from './stringifyOpenAPI';
@@ -99,9 +99,9 @@ export function OpenAPICodeSample(props) {
99
99
  return null;
100
100
  }
101
101
  return (<OpenAPITabs stateKey={createStateKey('codesample')} items={samples}>
102
- <InteractiveSection header={<OpenAPITabsList />} className="openapi-codesample">
102
+ <StaticSection header={<OpenAPITabsList />} className="openapi-codesample">
103
103
  <OpenAPITabsPanels />
104
- </InteractiveSection>
104
+ </StaticSection>
105
105
  </OpenAPITabs>);
106
106
  }
107
107
  function getSecurityHeaders(securities) {
@@ -1,3 +1,4 @@
1
+ 'use client';
1
2
  import { useRef } from 'react';
2
3
  import { mergeProps, useButton, useDisclosure, useFocusRing } from 'react-aria';
3
4
  import { useDisclosureState } from 'react-stately';
@@ -1,3 +1,4 @@
1
+ import { type DisclosureGroupProps } from 'react-stately';
1
2
  interface Props {
2
3
  groups: TDisclosureGroup[];
3
4
  icon?: React.ReactNode;
@@ -11,7 +12,6 @@ type TDisclosureGroup = {
11
12
  body?: React.ReactNode;
12
13
  }[];
13
14
  };
14
- import { type DisclosureGroupProps } from 'react-stately';
15
15
  /**
16
16
  * Display an interactive OpenAPI disclosure group.
17
17
  */
@@ -1,3 +1,4 @@
1
+ 'use client';
1
2
  var __assign = (this && this.__assign) || function () {
2
3
  __assign = Object.assign || function(t) {
3
4
  for (var s, i = 1, n = arguments.length; i < n; i++) {
@@ -1,5 +1,5 @@
1
- import { InteractiveSection } from './InteractiveSection';
2
1
  import { OpenAPITabs, OpenAPITabsList, OpenAPITabsPanels } from './OpenAPITabs';
2
+ import { StaticSection } from './StaticSection';
3
3
  import { generateSchemaExample } from './generateSchemaExample';
4
4
  import { json2xml } from './json2xml';
5
5
  import { stringifyOpenAPI } from './stringifyOpenAPI';
@@ -66,9 +66,9 @@ export function OpenAPIResponseExample(props) {
66
66
  return null;
67
67
  }
68
68
  return (<OpenAPITabs stateKey={createStateKey('response-example')} items={tabs}>
69
- <InteractiveSection header={<OpenAPITabsList />} className="openapi-response-example">
69
+ <StaticSection header={<OpenAPITabsList />} className="openapi-response-example">
70
70
  <OpenAPITabsPanels />
71
- </InteractiveSection>
71
+ </StaticSection>
72
72
  </OpenAPITabs>);
73
73
  }
74
74
  function OpenAPIResponse(props) {
@@ -91,9 +91,9 @@ function OpenAPIResponse(props) {
91
91
  };
92
92
  });
93
93
  return (<OpenAPITabs stateKey={createStateKey('response-media-types')} items={tabs}>
94
- <InteractiveSection header={<OpenAPITabsList />} className="openapi-response-media-types">
94
+ <StaticSection header={<OpenAPITabsList />} className="openapi-response-media-types">
95
95
  <OpenAPITabsPanels />
96
- </InteractiveSection>
96
+ </StaticSection>
97
97
  </OpenAPITabs>);
98
98
  }
99
99
  function OpenAPIResponseMediaType(props) {
@@ -111,13 +111,13 @@ function OpenAPIResponseMediaType(props) {
111
111
  return {
112
112
  key: example.key,
113
113
  label: example.example.summary || example.key,
114
- body: (<OpenAPIExample example={firstExample.example} context={props.context} syntax={syntax}/>),
114
+ body: (<OpenAPIExample example={example.example} context={props.context} syntax={syntax}/>),
115
115
  };
116
116
  });
117
117
  return (<OpenAPITabs stateKey={createStateKey('response-media-type-examples')} items={tabs}>
118
- <InteractiveSection header={<OpenAPITabsList />} className="openapi-response-media-type-examples">
118
+ <StaticSection header={<OpenAPITabsList />} className="openapi-response-media-type-examples">
119
119
  <OpenAPITabsPanels />
120
- </InteractiveSection>
120
+ </StaticSection>
121
121
  </OpenAPITabs>);
122
122
  }
123
123
  /**
@@ -1,13 +1,13 @@
1
- import { InteractiveSection } from './InteractiveSection';
2
1
  import { Markdown } from './Markdown';
3
2
  import { OpenAPIDisclosureGroup } from './OpenAPIDisclosureGroup';
4
3
  import { OpenAPIResponse } from './OpenAPIResponse';
4
+ import { StaticSection } from './StaticSection';
5
5
  /**
6
6
  * Display an interactive response body.
7
7
  */
8
8
  export function OpenAPIResponses(props) {
9
9
  var responses = props.responses, context = props.context;
10
- return (<InteractiveSection header="Responses" className="openapi-responses">
10
+ return (<StaticSection header="Responses" className="openapi-responses">
11
11
  <OpenAPIDisclosureGroup allowsMultipleExpanded icon={context.icons.chevronRight} groups={Object.entries(responses).map(function (_a) {
12
12
  var _b;
13
13
  var statusCode = _a[0], response = _a[1];
@@ -31,5 +31,5 @@ export function OpenAPIResponses(props) {
31
31
  }),
32
32
  };
33
33
  })}/>
34
- </InteractiveSection>);
34
+ </StaticSection>);
35
35
  }
@@ -51,7 +51,7 @@ export function OpenAPISchemaProperties(props) {
51
51
  return null;
52
52
  }
53
53
  return (<div id={id} className="openapi-schema-properties">
54
- {properties.map(function (property) { return (<OpenAPISchemaProperty key={property.propertyName} circularRefs={circularRefs} {...property} context={context}/>); })}
54
+ {properties.map(function (property, index) { return (<OpenAPISchemaProperty key={index} circularRefs={circularRefs} {...property} context={context}/>); })}
55
55
  </div>);
56
56
  }
57
57
  /**
@@ -1,9 +1,8 @@
1
- 'use client';
2
- import { InteractiveSection } from './InteractiveSection';
3
1
  import { OpenAPIRequestBody } from './OpenAPIRequestBody';
4
2
  import { OpenAPIResponses } from './OpenAPIResponses';
5
3
  import { OpenAPISchemaProperties } from './OpenAPISchema';
6
4
  import { OpenAPISecurities } from './OpenAPISecurities';
5
+ import { StaticSection } from './StaticSection';
7
6
  import { parameterToProperty } from './utils';
8
7
  /**
9
8
  * Client component to render the spec for the request and response.
@@ -21,9 +20,9 @@ export function OpenAPISpec(props) {
21
20
  {securities.length > 0 ? (<OpenAPISecurities securities={securities} context={context}/>) : null}
22
21
 
23
22
  {parameterGroups.map(function (group) {
24
- return (<InteractiveSection key={group.key} className="openapi-parameters" header={group.label}>
23
+ return (<StaticSection key={group.key} className="openapi-parameters" header={group.label}>
25
24
  <OpenAPISchemaProperties properties={group.parameters.map(parameterToProperty)} context={context}/>
26
- </InteractiveSection>);
25
+ </StaticSection>);
27
26
  })}
28
27
 
29
28
  {operation.requestBody ? (<OpenAPIRequestBody requestBody={operation.requestBody} context={context}/>) : null}
@@ -1,9 +1,9 @@
1
1
  'use client';
2
- import { createContext, useContext, useEffect, useMemo, useState } from 'react';
2
+ import { createContext, useContext, useEffect, useMemo, useRef, useState } from 'react';
3
3
  import { Tab, TabList, TabPanel, Tabs } from 'react-aria-components';
4
- import { useIntersectionObserver } from 'usehooks-ts';
4
+ import { useEventCallback } from 'usehooks-ts';
5
5
  import { Markdown } from './Markdown';
6
- import { useSyncedTabsGlobalState } from './useSyncedTabsGlobalState';
6
+ import { getOrCreateTabStoreByKey } from './useSyncedTabsGlobalState';
7
7
  var OpenAPITabsContext = createContext(null);
8
8
  function useOpenAPITabsContext() {
9
9
  var context = useContext(OpenAPITabsContext);
@@ -16,59 +16,62 @@ function useOpenAPITabsContext() {
16
16
  * The OpenAPI Tabs wrapper component.
17
17
  */
18
18
  export function OpenAPITabs(props) {
19
+ var _a, _b;
19
20
  var children = props.children, items = props.items, stateKey = props.stateKey;
20
- var _a = useIntersectionObserver({
21
- threshold: 0.1,
22
- rootMargin: '200px',
23
- }), ref = _a[0], isIntersectionVisible = _a[1];
24
- var isVisible = stateKey ? isIntersectionVisible : true;
25
- var defaultTab = items[0];
26
- var _b = useSyncedTabsGlobalState(), syncedTabs = _b[0], setSyncedTabs = _b[1];
27
21
  var _c = useState(function () {
28
- var _a, _b, _c;
29
- if (isVisible && stateKey && syncedTabs && syncedTabs.has(stateKey)) {
30
- var tabFromState = syncedTabs.get(stateKey);
31
- return (_a = tabFromState === null || tabFromState === void 0 ? void 0 : tabFromState.key) !== null && _a !== void 0 ? _a : (_b = items[0]) === null || _b === void 0 ? void 0 : _b.key;
32
- }
33
- return (_c = items[0]) === null || _c === void 0 ? void 0 : _c.key;
34
- }), selectedTabKey = _c[0], setSelectedTabKey = _c[1];
35
- var _d = useState(defaultTab), selectedTab = _d[0], setSelectedTab = _d[1];
36
- var handleSelectionChange = function (key) {
37
- setSelectedTabKey(key);
38
- if (stateKey) {
39
- var tab_1 = items.find(function (item) { return item.key === key; });
40
- if (!tab_1) {
41
- return;
22
+ var _a, _b;
23
+ if (stateKey && typeof window !== 'undefined') {
24
+ var store = getOrCreateTabStoreByKey(stateKey);
25
+ var tabKey_1 = store.getState().tabKey;
26
+ if (tabKey_1) {
27
+ return tabKey_1;
42
28
  }
43
- setSyncedTabs(function (state) {
44
- var newState = new Map(state);
45
- newState.set(stateKey, tab_1);
46
- return newState;
47
- });
48
29
  }
49
- };
30
+ return (_b = (_a = items[0]) === null || _a === void 0 ? void 0 : _a.key) !== null && _b !== void 0 ? _b : null;
31
+ }), tabKey = _c[0], setTabKey = _c[1];
32
+ var selectTab = useEventCallback(function (key) {
33
+ if (!key || key === tabKey) {
34
+ return;
35
+ }
36
+ var tab = items.find(function (item) { return item.key === key; });
37
+ if (!tab) {
38
+ return;
39
+ }
40
+ setTabKey(key);
41
+ });
42
+ var selectedTab = (_b = (_a = items.find(function (item) { return item.key === tabKey; })) !== null && _a !== void 0 ? _a : items[0]) !== null && _b !== void 0 ? _b : null;
43
+ var cancelDeferRef = useRef(null);
50
44
  useEffect(function () {
51
- if (isVisible && stateKey && syncedTabs && syncedTabs.has(stateKey)) {
52
- var tabFromState_1 = syncedTabs.get(stateKey);
53
- if (!items.some(function (item) { return item.key === (tabFromState_1 === null || tabFromState_1 === void 0 ? void 0 : tabFromState_1.key); })) {
54
- return setSelectedTab(defaultTab);
55
- }
56
- if (tabFromState_1 && (tabFromState_1 === null || tabFromState_1 === void 0 ? void 0 : tabFromState_1.key) !== (selectedTab === null || selectedTab === void 0 ? void 0 : selectedTab.key)) {
57
- var tabFromItems = items.find(function (item) { return item.key === tabFromState_1.key; });
58
- if (!tabFromItems) {
59
- return;
60
- }
61
- setSelectedTab(tabFromItems);
62
- }
45
+ if (!stateKey) {
46
+ return undefined;
63
47
  }
64
- }, [isVisible, stateKey, syncedTabs, selectedTabKey]);
48
+ var store = getOrCreateTabStoreByKey(stateKey);
49
+ return store.subscribe(function (state) {
50
+ var _a;
51
+ (_a = cancelDeferRef.current) === null || _a === void 0 ? void 0 : _a.call(cancelDeferRef);
52
+ cancelDeferRef.current = defer(function () { return selectTab(state.tabKey); });
53
+ });
54
+ }, [stateKey, selectTab]);
55
+ useEffect(function () {
56
+ return function () { var _a; return (_a = cancelDeferRef.current) === null || _a === void 0 ? void 0 : _a.call(cancelDeferRef); };
57
+ }, []);
65
58
  var contextValue = useMemo(function () { return ({ items: items, selectedTab: selectedTab }); }, [items, selectedTab]);
66
59
  return (<OpenAPITabsContext.Provider value={contextValue}>
67
- <Tabs ref={ref} className="openapi-tabs" onSelectionChange={handleSelectionChange} selectedKey={selectedTab === null || selectedTab === void 0 ? void 0 : selectedTab.key}>
60
+ <Tabs className="openapi-tabs" onSelectionChange={function (tabKey) {
61
+ selectTab(tabKey);
62
+ if (stateKey) {
63
+ var store = getOrCreateTabStoreByKey(stateKey);
64
+ store.setState({ tabKey: tabKey });
65
+ }
66
+ }} selectedKey={tabKey}>
68
67
  {children}
69
68
  </Tabs>
70
69
  </OpenAPITabsContext.Provider>);
71
70
  }
71
+ var defer = function (fn) {
72
+ var id = setTimeout(fn, 0);
73
+ return function () { return clearTimeout(id); };
74
+ };
72
75
  /**
73
76
  * The OpenAPI Tabs list component.
74
77
  * This component should be used as a child of the OpenAPITabs component.
@@ -77,14 +80,14 @@ export function OpenAPITabs(props) {
77
80
  export function OpenAPITabsList() {
78
81
  var items = useOpenAPITabsContext().items;
79
82
  return (<TabList className="openapi-tabs-list">
80
- {items.map(function (tab) { return (<Tab style={function (_a) {
83
+ {items.map(function (tab) { return (<Tab key={tab.key} id={tab.key} style={function (_a) {
81
84
  var isFocusVisible = _a.isFocusVisible;
82
85
  return ({
83
86
  outline: isFocusVisible
84
87
  ? '2px solid rgb(var(--primary-color-500)/0.4)'
85
88
  : 'none',
86
89
  });
87
- }} className="openapi-tabs-tab" key={"Tab-".concat(tab.key)} id={tab.key}>
90
+ }} className="openapi-tabs-tab">
88
91
  {tab.label}
89
92
  </Tab>); })}
90
93
  </TabList>);
@@ -99,7 +102,8 @@ export function OpenAPITabsPanels() {
99
102
  if (!selectedTab) {
100
103
  return null;
101
104
  }
102
- return (<TabPanel key={"TabPanel-".concat(selectedTab.key)} id={selectedTab.key.toString()} className="openapi-tabs-panel">
105
+ var key = selectedTab.key.toString();
106
+ return (<TabPanel key={key} id={key} className="openapi-tabs-panel">
103
107
  {selectedTab.body}
104
108
  {selectedTab.description ? (<Markdown source={selectedTab.description} className="openapi-tabs-footer"/>) : null}
105
109
  </TabPanel>);
@@ -0,0 +1,10 @@
1
+ import { type ComponentPropsWithoutRef } from 'react';
2
+ export declare function Section(props: ComponentPropsWithoutRef<'div'>): import("react").JSX.Element;
3
+ export declare function SectionHeader(props: ComponentPropsWithoutRef<'div'>): import("react").JSX.Element;
4
+ export declare function SectionHeaderContent(props: ComponentPropsWithoutRef<'div'>): import("react").JSX.Element;
5
+ export declare const SectionBody: import("react").ForwardRefExoticComponent<Omit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & import("react").RefAttributes<HTMLDivElement>>;
6
+ export declare function StaticSection(props: {
7
+ className: string;
8
+ header: React.ReactNode;
9
+ children: React.ReactNode;
10
+ }): import("react").JSX.Element;
@@ -0,0 +1,23 @@
1
+ import clsx from 'clsx';
2
+ import { forwardRef } from 'react';
3
+ export function Section(props) {
4
+ return <div {...props} className={clsx('openapi-section', props.className)}/>;
5
+ }
6
+ export function SectionHeader(props) {
7
+ return (<div {...props} className={clsx('openapi-section-header', props.className && "".concat(props.className, "-header"))}/>);
8
+ }
9
+ export function SectionHeaderContent(props) {
10
+ return (<div {...props} className={clsx('openapi-section-header-content', props.className && "".concat(props.className, "-header-content"))}/>);
11
+ }
12
+ export var SectionBody = forwardRef(function SectionBody(props, ref) {
13
+ return (<div ref={ref} {...props} className={clsx('openapi-section-body', props.className && "".concat(props.className, "-body"))}/>);
14
+ });
15
+ export function StaticSection(props) {
16
+ var className = props.className, header = props.header, children = props.children;
17
+ return (<Section className={className}>
18
+ <SectionHeader className={className}>
19
+ <SectionHeaderContent className={className}>{header}</SectionHeaderContent>
20
+ </SectionHeader>
21
+ <SectionBody className={className}>{children}</SectionBody>
22
+ </Section>);
23
+ }