MCPcopy Create free account
hub / github.com/adobe/react-spectrum / VisualExample

Function VisualExample

packages/dev/s2-docs/src/VisualExample.tsx:116–243  ·  view source on GitHub ↗
({
  name,
  component,
  docs,
  links,
  importSource,
  props,
  initialProps,
  controlOptions,
  files,
  downloadFiles,
  code,
  wide,
  slots,
  align,
  acceptOrientation,
  type,
  propsObject,
  showCoachMark,
  hideShadcn,
  hideCode
}: VisualExampleProps)

Source from the content-addressed store, hash-verified

114 * Displays a component example with controls for changing the props.
115 */
116export function VisualExample({
117 name,
118 component,
119 docs,
120 links,
121 importSource,
122 props,
123 initialProps,
124 controlOptions,
125 files,
126 downloadFiles,
127 code,
128 wide,
129 slots,
130 align,
131 acceptOrientation,
132 type,
133 propsObject,
134 showCoachMark,
135 hideShadcn,
136 hideCode
137}: VisualExampleProps) {
138 let componentProps = docs.type === 'interface' ? docs : docs.props;
139 if (componentProps?.type !== 'interface') {
140 return null;
141 }
142
143 // Filter down the list of controls from the TS docs to only the ones we want to display.
144 // This reduces the amount of data we need to send to the client.
145 let controls = Object.fromEntries(
146 props.map(name => {
147 let prop = componentProps.properties[name];
148 if (prop.type === 'method') {
149 throw new Error('Unexpected method in props.');
150 }
151
152 // Resolve the value type if it is a type alias.
153 if (prop.value?.type === 'link' && links?.[prop.value.id]) {
154 let value = links[prop.value.id];
155 if (value?.type === 'alias') {
156 value = value.value;
157 }
158 prop = {...prop, value};
159 }
160
161 // Try to parse the default value from the JSDocs as JSON.
162 let defaultValue = prop.default ?? undefined;
163 if (typeof defaultValue === 'string') {
164 defaultValue = defaultValue.replace(/^['"](.+)['"].*$/, '"$1"');
165 try {
166 defaultValue = json5.parse(defaultValue);
167 } catch {
168 // ignore
169 }
170 }
171
172 let renderedProp: PropControl = {
173 ...prop,

Callers

nothing calls this directly

Calls 3

renderHTMLfromMarkdownFunction · 0.90
getFilesFunction · 0.90
parseMethod · 0.45

Tested by

no test coverage detected