Skip to content

Commit 589d49b

Browse files
authored
fix: allow to display the code samples when try it panel is hidden (#2624)
* fix: allow to display the code samples when try it panel is hidden The `TryIt`-component is responsible for generating the request data that is used to render the request sample. This requires the component to also be rendered when we don't want to display the TryIt panel itself. * fix: resolve the issue with displaying `TryIt`-component * docs: update the documentation by adding call out for `hideTryItPanel`-option ---------
1 parent c4fc263 commit 589d49b

File tree

5 files changed

+26
-6
lines changed

5 files changed

+26
-6
lines changed

docs/getting-started/elements/elements-options.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
- `apiDescriptionDocument` - OpenAPI document, provided as YAML string, JSON string, or JavaScript object.
55
- `basePath` - Helps when using `router: 'history'` but docs are in a subdirectory like `https://example.com/docs/api`.
66
- `hideInternal` - Pass `"true"` to filter out any content which has been marked as internal with `x-internal`.
7-
- `hideTryIt` - Pass `true` to hide the [**Try It** feature](https://docs.stoplight.io/docs/platform/ZG9jOjM2OTM3Mjky-try-it).
7+
- `hideTryIt` - Pass `true` to hide the [**Try It** feature](https://docs.stoplight.io/docs/platform/ZG9jOjM2OTM3Mjky-try-it) completely.
8+
- `hideTryItPanel` - Pass `true` to hide the Try It panel while still display the Request Sample, expects `hideTryIt` to be `false`
89
- `hideSchemas` - Pass `true` to hide the schemas in the Table of Contents, when using the `sidebar` layout.
910
- `hideExport` - Pass `true` to hide the Export button on overview section of the documentation.
1011
- `tryItCorsProxy` - Pass the URL of a CORS proxy used to send requests to the Try It feature. The provided URL is pre-pended to the URL of an actual request.
@@ -19,4 +20,3 @@
1920
- `hash` - uses the hash portion of the URL to keep the UI in sync with the URL.
2021
- `memory` - keeps the history of your "URL" in memory (doesn't read or write to the address bar).
2122
- `static` - renders using the StaticRouter which can help render pages on the server.
22-

packages/elements-core/src/components/Docs/HttpOperation/HttpOperation.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ const HttpOperationComponent = React.memo<HttpOperationProps>(
7979
responseStatusCode={responseStatusCode}
8080
requestBodyIndex={requestBodyIndex}
8181
hideTryIt={layoutOptions?.hideTryIt}
82+
hideTryItPanel={layoutOptions?.hideTryItPanel}
8283
hideSamples={layoutOptions?.hideSamples}
8384
tryItCredentialsPolicy={tryItCredentialsPolicy}
8485
mockUrl={mocking.hideMocking ? undefined : mocking.mockUrl}

packages/elements-core/src/components/TryIt/TryIt.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ export interface TryItProps {
5656
*/
5757
embeddedInMd?: boolean;
5858

59+
/**
60+
* True when TryIt Panel should be hidden
61+
*/
62+
hideTryItPanel?: boolean;
63+
5964
/**
6065
* Fetch credentials policy for TryIt component
6166
* For more information: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
@@ -78,6 +83,7 @@ export const TryIt: React.FC<TryItProps> = ({
7883
onRequestChange,
7984
requestBodyIndex,
8085
embeddedInMd = false,
86+
hideTryItPanel = false,
8187
tryItCredentialsPolicy,
8288
corsProxy,
8389
}) => {
@@ -349,7 +355,7 @@ export const TryIt: React.FC<TryItProps> = ({
349355

350356
return (
351357
<Box rounded="lg" overflowY="hidden">
352-
{tryItPanelElem}
358+
{hideTryItPanel ? null : tryItPanelElem}
353359
{requestData && embeddedInMd && (
354360
<RequestSamples request={requestData} customCodeSamples={customCodeSamples} embeddedInMd />
355361
)}

packages/elements-core/src/components/TryIt/TryItWithRequestSamples.stories.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ WithVariables.args = {
2323
httpOperation: operationWithUrlVariables,
2424
};
2525
WithVariables.storyName = 'With Server Variables';
26+
27+
export const WithoutTryItPanel = Template.bind({});
28+
WithoutTryItPanel.args = {
29+
hideTryIt: true,
30+
httpOperation: operationWithUrlVariables,
31+
};
32+
WithoutTryItPanel.storyName = 'Without Try It Panel';

packages/elements-core/src/components/TryIt/TryItWithRequestSamples.tsx

+9-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import { ResponseExamples, ResponseExamplesProps } from '../ResponseExamples/Res
77
import { TryIt, TryItProps } from './TryIt';
88

99
export type TryItWithRequestSamplesProps = Omit<TryItProps, 'onRequestChange'> &
10-
ResponseExamplesProps & { hideTryIt?: boolean; hideSamples?: boolean };
10+
ResponseExamplesProps & { hideTryIt?: boolean; hideTryItPanel?: boolean; hideSamples?: boolean };
1111

1212
export const TryItWithRequestSamples: React.FC<TryItWithRequestSamplesProps> = ({
1313
hideTryIt,
14+
hideTryItPanel,
1415
hideSamples,
1516
...props
1617
}) => {
@@ -20,12 +21,17 @@ export const TryItWithRequestSamples: React.FC<TryItWithRequestSamplesProps> = (
2021

2122
return (
2223
<VStack spacing={6}>
23-
{!hideTryIt && (
24+
{!hideTryIt ? (
2425
<InvertTheme>
2526
<Box>
26-
<TryIt {...props} onRequestChange={setRequestData} />
27+
<TryIt {...props} hideTryItPanel={hideTryItPanel} onRequestChange={setRequestData} />
2728
</Box>
2829
</InvertTheme>
30+
) : (
31+
// The TryIt is responsible for generating the Request Data so it should always be rendered
32+
<>
33+
<TryIt {...props} hideTryItPanel={hideTryIt} onRequestChange={setRequestData} />
34+
</>
2935
)}
3036

3137
{requestData && !hideSamples && <RequestSamples request={requestData} customCodeSamples={customCodeSamples} />}

0 commit comments

Comments
 (0)