Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated CodeSanboxes #403

Merged
merged 47 commits into from
Dec 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
d06bb6d
Scaffold React sandboxes and playground
davidjerleke Nov 8, 2022
0a12753
Rename examples to sandboxes
davidjerleke Nov 8, 2022
8aae6c1
Add all examples to react sandboxes playground
davidjerleke Nov 12, 2022
07558fc
Structure sandboxes and move all to one page
davidjerleke Nov 13, 2022
ddab830
Finish TypeScript compile scripts for React sandboxes
davidjerleke Nov 16, 2022
05ff66b
Fix ts error in ios picker sandbox
davidjerleke Nov 16, 2022
0359515
Add prettier and make dynamical sandbox work
davidjerleke Nov 20, 2022
286a61c
Add dynamic prettier loader
davidjerleke Nov 20, 2022
02cdb3e
Load sandbox files on demand
davidjerleke Nov 22, 2022
731be64
Fix broken react example playground
davidjerleke Nov 22, 2022
ae26cb5
Add TypeScript support to creating sandboxes
davidjerleke Nov 23, 2022
79da3f3
Get rid of rendering issues on scale example
davidjerleke Nov 23, 2022
2063aa7
Add React & React+TS sandboxes for all Basic examples
davidjerleke Nov 23, 2022
bc98a56
Add React sandboxes for Navigation and Plugin examples
davidjerleke Nov 24, 2022
8561310
Create examples for all Misc except ios picker
davidjerleke Nov 25, 2022
dbfc665
Pick website theme when creating sandbox, add ios-picker entry files
davidjerleke Nov 26, 2022
1a1ec67
Add CSS variables to examples and get rid of height: 100% on many places
davidjerleke Nov 26, 2022
e9257b4
Start building vanilla sandbox utils
davidjerleke Nov 26, 2022
bdb9d74
Add declarations to vanilla sandbox files
davidjerleke Nov 26, 2022
703b3e1
Structure sandbox types/utils/regex better and scaffold vanilla creat…
davidjerleke Nov 27, 2022
4c0cb9d
Stop flex slide content from overflowing
davidjerleke Nov 27, 2022
199dc70
Make first dynamic vanilla codesandbox work
davidjerleke Nov 27, 2022
c89189d
Move sandbox files and scripts to the embla-carousel-docs workspace
davidjerleke Nov 29, 2022
62f3110
Setup React playground and fix wrong example paths
davidjerleke Nov 29, 2022
618ad8c
Setup vanilla playground
davidjerleke Nov 29, 2022
9e6abdb
Get rid of custom sandbox ios picker css class
davidjerleke Nov 30, 2022
9902e5f
Add create sandbox functions for all basic vanilla examples
davidjerleke Nov 30, 2022
263dc46
Add vanilla sandboxes to all except miscellaneous
davidjerleke Dec 2, 2022
2c8374c
Add progress to vanilla sandboxes
davidjerleke Dec 2, 2022
1510b3a
Add TODO comments
davidjerleke Dec 2, 2022
1ac10aa
Add lazy load to vanilla sandboxes
davidjerleke Dec 2, 2022
87bd478
Add infinite scroll to vanilla sandboxes
davidjerleke Dec 4, 2022
3fd64d4
Implement new folder structure to avoid many import re-writes
davidjerleke Dec 4, 2022
ad544e9
Finish IOS picker examples
davidjerleke Dec 8, 2022
0ac5ff3
Add prettier format safe to prevent page crash
davidjerleke Dec 8, 2022
92ae0e8
Move sandbox labels to a constant
davidjerleke Dec 8, 2022
8d4c11b
Add constants for sandbox folders
davidjerleke Dec 9, 2022
9f83f56
Remove console for every sandbox file and add colors to sandbox creat…
davidjerleke Dec 9, 2022
1bbfa5d
Improve sandbox labels with createSandboxFunctionsWithLabels
davidjerleke Dec 9, 2022
933cd60
Make it easier to add plugins to sandboxes
davidjerleke Dec 9, 2022
ca9b164
Fix vertical carousel badge
davidjerleke Dec 9, 2022
4568481
Remove lookahead regex
davidjerleke Dec 10, 2022
e71e17e
Format files and get rid of lint errors
davidjerleke Dec 10, 2022
616cb8c
Remove TODO comment
davidjerleke Dec 10, 2022
865a99c
Remove webpack artefact config, add sandbox files build command to do…
davidjerleke Dec 10, 2022
e6a7ecd
Add format to build script
davidjerleke Dec 11, 2022
63f9b26
Update husky pre-push hook
davidjerleke Dec 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add dynamic prettier loader
  • Loading branch information
davidjerleke committed Nov 20, 2022
commit 286a61c75076570009983e0e8310b098d105fd10
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import React, { useCallback, useEffect, useRef, useState } from 'react'
import { createSandboxReactPackageJson } from '../createSandboxReactPackageJson'
import { createSandboxReact } from '../createSandboxReact'
import { useRoutes } from 'hooks/useRoutes'

const CarouselJs: string =
require('!!raw-loader!embla-carousel-react-sandboxes/src/SandboxFilesDist/CarouselDefault.jsx').default

const packageJson = createSandboxReactPackageJson({
name: 'embla-carousel-react-default',
})

export const createSandboxReactDefault = async (): Promise<string> =>
export const createSandboxReactDefault = (): Promise<string> =>
createSandboxReact(packageJson, CarouselJs)

export const CreateSandboxReactDefault = () => {
const { setIsLoading } = useRoutes()
const [sandbox, setSandbox] = useState('')
const formRef = useRef<HTMLFormElement>(null)

Expand All @@ -20,15 +23,18 @@ export const CreateSandboxReactDefault = () => {
event.preventDefault()
if (sandbox) return formRef.current?.submit()

setIsLoading(true)
const codeSandbox = await createSandboxReactDefault()
setSandbox(codeSandbox)
},
[sandbox],
[sandbox, setIsLoading],
)

useEffect(() => {
if (sandbox) formRef.current?.submit()
}, [sandbox])
if (!sandbox) return
formRef.current?.submit()
setIsLoading(false)
}, [sandbox, setIsLoading])

return (
<form
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { EmblaOptionsType } from 'embla-carousel-react'
import prettierrc from '../../../../../.prettierrc'
import { getParameters } from 'codesandbox/lib/api/define'
import { PackageJsonType, SandboxConfigType } from './types'
import { resetStyles } from 'components/Layout/GlobalStyles/reset'
import { baseStyles } from 'components/Layout/GlobalStyles/base'
import { fontStyles } from 'components/Layout/GlobalStyles/font'
import { themeStyles } from 'consts/themes'
import { Options as PretterOptions } from 'prettier'
import { styledComponentsStylesToString } from 'utils/styledComponentStylesToString'
import { createSandboxReactIndexHtml } from './createSandboxReactIndexHtml'
import { createSandboxReactDefaultEntry } from './createSandboxReactEntry'
import { createCarouselDefaultStyles } from 'components/Examples/createCarouselStyles'
import { createSandboxReactHeader } from './createSandboxReactHeader'
import { createSandboxReactFooter } from './createSandboxReactFooter'
import { createSandboxReactImages } from './createSandboxReactImages'
import { loadPrettier } from 'utils/loadPrettier'
import { URLS } from 'consts/urls'
import {
sandboxCarouselStyles,
Expand All @@ -22,8 +21,6 @@ import {
sandboxWrapperStyles,
} from 'components/Examples/sandboxStyles'

const PRETTIER_CONFIG = <PretterOptions>prettierrc

const BASE_CSS = styledComponentsStylesToString(
themeStyles,
resetStyles,
Expand All @@ -37,6 +34,10 @@ const SANDBOX_CSS = styledComponentsStylesToString(
sandboxFooterStyles,
)

const SANDBOX_CAROUSEL_CSS = styledComponentsStylesToString(
sandboxCarouselStyles,
)

export const createSandboxReact = async (
packageJson: PackageJsonType,
carousel: string,
Expand All @@ -45,23 +46,12 @@ export const createSandboxReact = async (
styles: string = createCarouselDefaultStyles(),
customConfig?: SandboxConfigType['files'],
): Promise<string> => {
const prettier = await import('prettier')
const prettierPostCssParser = await import('prettier/parser-postcss')
const prettierBabelParser = await import('prettier/parser-babel')

const emblaCss = prettier.format(
styledComponentsStylesToString(sandboxCarouselStyles) + styles,
{
...PRETTIER_CONFIG,
parser: 'css',
plugins: [prettierPostCssParser],
},
)
const { prettierConfig, formatCss, formatJs } = await loadPrettier()

const DEFAULT_CONFIG: SandboxConfigType['files'] = {
'.prettierrc': {
isBinary: false,
content: JSON.stringify(prettierrc, null, '\t'),
content: JSON.stringify(prettierConfig, null, '\t'),
},
'package.json': {
isBinary: false,
Expand All @@ -73,39 +63,35 @@ export const createSandboxReact = async (
},
'src/css/base.css': {
isBinary: false,
content: BASE_CSS,
content: formatCss(BASE_CSS),
},
'src/css/sandbox.css': {
isBinary: false,
content: SANDBOX_CSS,
content: formatCss(SANDBOX_CSS),
},
'src/css/embla.css': {
isBinary: false,
content: emblaCss,
content: formatCss(SANDBOX_CAROUSEL_CSS + styles),
},
'src/js/index.js': {
isBinary: false,
content: createSandboxReactDefaultEntry(slideCount, options),
content: formatJs(createSandboxReactDefaultEntry(slideCount, options)),
},
'src/js/Header.js': {
isBinary: false,
content: createSandboxReactHeader(packageJson.name),
content: formatJs(createSandboxReactHeader(packageJson.name)),
},
'src/js/Footer.js': {
isBinary: false,
content: createSandboxReactFooter(),
content: formatJs(createSandboxReactFooter()),
},
'src/js/EmblaCarousel.js': {
isBinary: false,
content: prettier.format(carousel, {
...PRETTIER_CONFIG,
parser: 'babel',
plugins: [prettierBabelParser],
}),
content: formatJs(carousel),
},
'src/js/imageByIndex.js': {
isBinary: false,
content: createSandboxReactImages(),
content: formatJs(createSandboxReactImages()),
},
'src/images/slide-1.jpg': {
isBinary: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const sandboxReactPackageJsonTemplate: PackageJsonType = {
}

export const createSandboxReactPackageJson = (
customConfig: Record<string, unknown>,
customConfig: Partial<PackageJsonType>,
): PackageJsonType => {
return Object.assign({}, sandboxReactPackageJsonTemplate, customConfig)
}
35 changes: 35 additions & 0 deletions packages/embla-carousel-docs/src/utils/loadPrettier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import prettierrc from '../../.prettierrc'
import { Options as PretterOptions } from 'prettier'

const PRETTIER_CONFIG = <PretterOptions>prettierrc

export const loadPrettier = async () => {
const prettier = await import('prettier')
const cssParser = await import('prettier/parser-postcss')
const babelParser = await import('prettier/parser-babel')

const prettierCssParser: PretterOptions = {
...PRETTIER_CONFIG,
parser: 'css',
plugins: [cssParser],
}

const prettierBabelParser: PretterOptions = {
...PRETTIER_CONFIG,
parser: 'babel',
plugins: [babelParser],
}

const formatCss = (css: string): string =>
prettier.format(css, prettierCssParser)

const formatJs = (js: string): string =>
prettier.format(js, prettierBabelParser)

return {
prettier,
prettierConfig: PRETTIER_CONFIG,
formatCss,
formatJs,
}
}