@@ -7,27 +7,30 @@ import {createStackBlitz} from './StackBlitz';
77import Download from '@react-spectrum/s2/icons/Download' ;
88import { iconStyle , style } from '@react-spectrum/s2/style' with { type : 'macro' } ;
99import { Key } from 'react-aria' ;
10+ import { Library } from './library' ;
1011import LinkIcon from '@react-spectrum/s2/icons/Link' ;
1112import OpenIn from '@react-spectrum/s2/icons/OpenIn' ;
1213import Polygon4 from '@react-spectrum/s2/icons/Polygon4' ;
1314import Prompt from '@react-spectrum/s2/icons/Prompt' ;
14- import React , { ReactNode , useEffect , useRef , useState } from 'react' ;
15+ import React , { createContext , ReactNode , useContext , useEffect , useRef , useState } from 'react' ;
1516import { zip } from './zip' ;
1617
1718const platterStyle = style ( {
1819 backgroundColor : 'layer-2' ,
1920 borderRadius : 'lg' ,
20- '--code-padding-x' : {
21- type : 'paddingTop' ,
21+ '--code-padding-start' : {
22+ type : 'paddingStart' ,
23+ value : 16
24+ } ,
25+ '--code-padding-end' : {
26+ type : 'paddingEnd' ,
2227 value : 16
2328 } ,
2429 '--code-padding-y' : {
2530 type : 'paddingTop' ,
2631 value : 16
2732 } ,
28- position : 'relative' ,
29- maxHeight : 600 ,
30- overflow : 'auto'
33+ position : 'relative'
3134} ) ;
3235
3336interface CodePlatterProps {
@@ -38,21 +41,38 @@ interface CodePlatterProps {
3841 registryUrl ?: string
3942}
4043
44+ interface CodePlatterContextValue {
45+ library : Library
46+ }
47+
48+ const CodePlatterContext = createContext < CodePlatterContextValue > ( { library : 'react-spectrum' } ) ;
49+ export function CodePlatterProvider ( props : CodePlatterContextValue & { children : any } ) {
50+ return < CodePlatterContext . Provider value = { props } > { props . children } </ CodePlatterContext . Provider > ;
51+ }
52+
4153export function CodePlatter ( { children, shareUrl, files, type, registryUrl} : CodePlatterProps ) {
4254 let codeRef = useRef < HTMLDivElement | null > ( null ) ;
4355 let [ showShadcn , setShowShadcn ] = useState ( false ) ;
4456 let getText = ( ) => codeRef . current ! . querySelector ( 'pre' ) ! . textContent ! ;
57+ let { library} = useContext ( CodePlatterContext ) ;
58+ if ( ! type ) {
59+ if ( library === 'react-aria' ) {
60+ type = 'vanilla' ;
61+ } else if ( library === 'react-spectrum' ) {
62+ type = 's2' ;
63+ }
64+ }
4565
4666 return (
4767 < div className = { platterStyle } >
48- < div className = { style ( { display : 'flex' , justifyContent : 'end' , float : 'inline-end ', padding : 16 , position : 'relative ' , zIndex : 1 } ) } >
68+ < div className = { style ( { display : 'flex' , justifyContent : 'end' , padding : 4 , position : 'absolute' , top : 8 , insetEnd : 8 , backgroundColor : 'layer-2 ', boxShadow : 'elevated' , borderRadius : 'default ' , zIndex : 1 } ) } >
4969 < ActionButtonGroup
5070 orientation = "vertical"
5171 isQuiet
5272 density = "regular"
5373 size = "S" >
5474 < CopyButton ariaLabel = "Copy code" tooltip = "Copy code" getText = { getText } />
55- { ( shareUrl || files || type || registryUrl ) && < MenuTrigger >
75+ { ( shareUrl || files || type || registryUrl ) && < MenuTrigger align = "end" >
5676 < TooltipTrigger placement = "end" >
5777 < ActionButton aria-label = "Open in…" >
5878 < OpenIn />
@@ -167,9 +187,23 @@ export function CodePlatter({children, shareUrl, files, type, registryUrl}: Code
167187 ) ;
168188}
169189
190+ const pre = style ( {
191+ borderRadius : 'lg' ,
192+ font : {
193+ default : 'code-xs' ,
194+ lg : 'code-sm'
195+ } ,
196+ margin : 0 ,
197+ paddingStart : '--code-padding-start' ,
198+ paddingEnd : '--code-padding-end' ,
199+ paddingY : '--code-padding-y' ,
200+ width : 'fit' ,
201+ minWidth : 'full'
202+ } ) ;
203+
170204export function Pre ( { children} ) {
171205 return (
172- < pre className = { style ( { borderRadius : 'lg' , font : { default : 'code-xs' , lg : 'code-sm' } , whiteSpace : ' pre-wrap' , margin : 0 , paddingX : '--code-padding-x' , paddingY : '--code-padding-y' } ) } style = { { overflowWrap : 'break-word' } } >
206+ < pre className = { pre } >
173207 { children }
174208 </ pre >
175209 ) ;
@@ -250,7 +284,7 @@ function ShadcnDialog({registryUrl}) {
250284 flexDirection : 'column' ,
251285 gap : 16
252286 } ) } >
253- < SegmentedControl selectedKey = { packageManager } onSelectionChange = { onSelectionChange } >
287+ < SegmentedControl aria-label = "Package manager" selectedKey = { packageManager } onSelectionChange = { onSelectionChange } >
254288 < SegmentedControlItem id = "npm" > npm</ SegmentedControlItem >
255289 < SegmentedControlItem id = "yarn" > yarn</ SegmentedControlItem >
256290 < SegmentedControlItem id = "pnpm" > pnpm</ SegmentedControlItem >
0 commit comments