Skip to content
This repository has been archived by the owner on Nov 17, 2021. It is now read-only.

Commit

Permalink
feat(Demo): Add theme contribution modal
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ketch committed Mar 5, 2020
1 parent e4ea41e commit 9bae762
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 71 deletions.
12 changes: 10 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"dependencies": {
"@simonwep/pickr": "^1.5.1",
"@stencila/components": "^0.9.0",
"project-name-generator": "^2.1.7",
"react": "^16.13.0",
"react-dom": "^16.13.0"
},
Expand Down
93 changes: 93 additions & 0 deletions src/demo/editor/contributeModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// @ts-ignore
import generate from 'project-name-generator'
import React from 'react'
import ReactDOM from 'react-dom'
import { submitPR, ThemeObject } from '../utils'

const randomName = (): string => generate().dashed

interface Props {
baseTheme: ThemeObject
baseThemeName: string
themeOverrides: ThemeObject
}

interface State {
projectName: string
}

export class ContributeForm extends React.Component<Props, State> {
private el: HTMLElement | null

constructor(props: Props) {
super(props)

this.el = document.getElementById('modalTarget')

this.state = {
projectName: randomName()
}
}

setProjectName = (projectName: string): void => {
this.setState({ projectName })
this.el = document.getElementById('modal')
}

onNameChange = (e: React.ChangeEvent<HTMLInputElement>): void => {
e.preventDefault()
this.setProjectName(e.currentTarget.value)
}

onRandomizeName = (e: React.MouseEvent<HTMLButtonElement>): void => {
e.preventDefault()
this.setProjectName(randomName())
}

onSubmitTheme = (e: React.MouseEvent<HTMLButtonElement>): void => {
e.preventDefault()

submitPR(
this.state.projectName,
'',
this.props.themeOverrides,
this.props.baseThemeName,
this.props.baseTheme
)
}

render(): React.ReactPortal | null {
return this.el === null
? null
: ReactDOM.createPortal(
<div id="contributeModal">
<div className="modalContents">
<p>
Name your theme, and submit as a GitHub pull request to share
your theme with others.
</p>

<form>
<label>Theme Name</label>

<div className="labelWrapper">
<input
value={this.state.projectName}
onChange={this.onNameChange}
/>

<button type="button" onClick={this.onRandomizeName}>
Randomize
</button>
</div>

<button onClick={this.onSubmitTheme} type="submit">
Open Pull Request
</button>
</form>
</div>
</div>,
this.el
)
}
}
10 changes: 5 additions & 5 deletions src/demo/editor/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { HeaderBase } from './headerBase'

interface Props {
/* onExampleChange: (e: React.ChangeEvent<HTMLSelectElement>) => string */
examples: string[]
exampleContent: string[]
/* themes: string[] */
}

const HeaderComponent = ({ examples }: Props): JSX.Element => {
const HeaderComponent = ({ exampleContent }: Props): JSX.Element => {
const [example, updateExample] = React.useState<string>(getExample())

return (
Expand All @@ -32,8 +32,8 @@ const HeaderComponent = ({ examples }: Props): JSX.Element => {
setExample(e.currentTarget.value)
}}
>
{examples.map(example => (
<option key={example}>{example}</option>
{exampleContent.map(ex => (
<option key={ex}>{ex}</option>
))}
</select>
</label>
Expand Down Expand Up @@ -62,7 +62,7 @@ export class Header extends React.Component {
return this.el === null
? null
: ReactDOM.createPortal(
<HeaderComponent examples={Object.keys(examples)} />,
<HeaderComponent exampleContent={Object.keys(examples)} />,
this.el
)
}
Expand Down
14 changes: 14 additions & 0 deletions src/demo/editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ import { getTheme } from '../utils/theme'
import { Header } from './header'
import { ThemeSwitcher } from './themeSwitcher'
import { VariableKnobs } from './variables/form'
import { ContributeForm } from './contributeModal'
import { ThemeObject } from '../utils'

export const ThemeEditor = (): JSX.Element => {
const [activeTheme, setTheme] = useState<string>(getTheme())
const [contributeModalIsOpen, toggleContributeModal] = useState<boolean>(true)
const [themeOverrides, setThemeOverrides] = useState<
Record<string, ThemeObject>
>({})

return (
<>
Expand All @@ -25,6 +31,14 @@ export const ThemeEditor = (): JSX.Element => {
<h3>Customize</h3>

<VariableKnobs theme={activeTheme} />

{contributeModalIsOpen && (
<ContributeForm
themeOverrides={themeOverrides[activeTheme] ?? {}}
baseTheme={themeOverrides[activeTheme] ?? {}}
baseThemeName={activeTheme}
/>
)}
</>
)
}
29 changes: 16 additions & 13 deletions src/demo/editor/variables/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,22 @@ export const VariableKnobs = ({ theme }: Props): JSX.Element => {

{userVars[theme] !== undefined &&
Object.keys(userVars[theme]).length > 0 && (
<a
className="button"
href="#"
id="contribute"
target="_blank"
title="Submit your changes as a new theme to Thema"
onClick={e => {
e.preventDefault()
submitPR('', '', userVars[theme], theme, themeVars[theme])
}}
>
Contribute changes
</a>
<>
<a
className="button"
href="#"
id="contribute"
target="_blank"
title="Submit your changes as a new theme to Thema"
onClick={e => {
e.preventDefault()
submitPR('', '', userVars[theme], theme, themeVars[theme])
}}
>
Contribute changes
</a>
<div id="contributeCover"></div>
</>
)}
</div>
)
Expand Down
Loading

0 comments on commit 9bae762

Please sign in to comment.