Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

feat(portal): simple base implementation #144

Merged
merged 2 commits into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Add Button `text` prop @mnajdova ([#177](https://github.com/stardust-ui/react/pull/177))
- Add accessibility keyboard action handlers @sophieH29 ([#121](https://github.com/stardust-ui/react/pull/121))
- Add accessibility description for `Text` component @codepretty ([#205](https://github.com/stardust-ui/react/pull/205))
- Add `Portal`, `PortalInner` and `Ref` components base implementation @Bugaa92 ([#144](https://github.com/stardust-ui/react/pull/144))

<!--------------------------------[ v0.5.0 ]------------------------------- -->
## [v0.5.0](https://github.com/stardust-ui/react/tree/v0.5.0) (2018-08-30)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React from 'react'
import { Button, Divider, Header, Label, Portal } from '@stardust-ui/react'

class PortalExamplePortal extends React.Component {
state = { log: [], logCount: 0 }

handleClick = () =>
this.setState({
log: [`${new Date().toLocaleTimeString()}: handleClick`, ...this.state.log].slice(0, 20),
logCount: this.state.logCount + 1,
})

clearLog = () => this.setState({ log: [], logCount: 0 })

render() {
const { log, logCount } = this.state

return (
<div>
<Portal
content={
<div
style={{
background: '#ddd',
position: 'fixed',
left: '40%',
top: '45%',
zIndex: 1000,
}}
>
<Header>This is a basic portal</Header>
<p>Portals have tons of great callback functions to hook into.</p>
<p>To close, simply click the close button or click away</p>
</div>
}
trigger={<Button content={'Toggle portal'} onClick={this.handleClick} />}
/>
<Divider />
<div>
<Button size="small" onClick={this.clearLog} content="Clear" />
<span>
Event Log <Label circular>{logCount}</Label>
</span>
<pre>{log.map((e, i) => <div key={i}>{e}</div>)}</pre>
</div>
</div>
)
}
}

export default PortalExamplePortal
48 changes: 48 additions & 0 deletions docs/src/examples/components/Portal/Types/PortalExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react'
import { Button, Divider, Header, Label, Portal } from '@stardust-ui/react'

class PortalExamplePortal extends React.Component {
state = { log: [], logCount: 0 }

handleClick = () =>
this.setState({
log: [`${new Date().toLocaleTimeString()}: handleClick`, ...this.state.log].slice(0, 20),
logCount: this.state.logCount + 1,
})

clearLog = () => this.setState({ log: [], logCount: 0 })

render() {
const { log, logCount } = this.state

return (
<div>
<Portal trigger={<Button content={'Toggle portal'} onClick={this.handleClick} />}>
<div
style={{
background: '#ddd',
position: 'fixed',
left: '40%',
top: '45%',
zIndex: 1000,
}}
>
<Header>This is a basic portal</Header>
<p>Portals have tons of great callback functions to hook into.</p>
<p>To close, simply click the close button or click away</p>
</div>
</Portal>
<Divider />
<div>
<Button size="small" onClick={this.clearLog} content="Clear" />
<span>
Event Log <Label circular>{logCount}</Label>
</span>
<pre>{log.map((e, i) => <div key={i}>{e}</div>)}</pre>
</div>
</div>
)
}
}

export default PortalExamplePortal
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React from 'react'
import { Button, Divider, Header, Label, Portal } from '@stardust-ui/react'

class PortalExampleControlled extends React.Component {
state = { log: [], logCount: 0, open: false }

handleClick = (logContent: string) => {
this.setState({ open: !this.state.open })
this.writeLog(logContent)
}

clearLog = () => this.setState({ log: [], logCount: 0 })

writeLog = eventName =>
this.setState({
log: [`${new Date().toLocaleTimeString()}: ${eventName}`, ...this.state.log].slice(0, 20),
logCount: this.state.logCount + 1,
})

render() {
const { log, logCount, open } = this.state
const btnContent = open ? 'Close Portal' : 'Open Portal'

return (
<div>
<div>
<Button content={btnContent} onClick={this.handleClick.bind(this, btnContent)} />
<Divider />
<Button size="small" onClick={this.clearLog} content="Clear" />
<span>
Event Log <Label circular>{logCount}</Label>
</span>
<pre>{log.map((e, i) => <div key={i}>{e}</div>)}</pre>
</div>
<Portal
open={open}
content={
<div
style={{
background: '#ddd',
position: 'fixed',
left: '40%',
top: '45%',
zIndex: 1000,
}}
>
<Header>This is a controlled portal</Header>
<p>Portals have tons of great callback functions to hook into.</p>
<p>To close, simply click the close button</p>
</div>
}
/>
</div>
)
}
}

export default PortalExampleControlled
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from 'react'
import { Button, Divider, Header, Label, Portal } from '@stardust-ui/react'

class PortalExampleControlled extends React.Component {
state = { log: [], logCount: 0, open: false }

handleClick = (logContent: string) => {
this.setState({ open: !this.state.open })
this.writeLog(logContent)
}

clearLog = () => this.setState({ log: [], logCount: 0 })

writeLog = eventName =>
this.setState({
log: [`${new Date().toLocaleTimeString()}: ${eventName}`, ...this.state.log].slice(0, 20),
logCount: this.state.logCount + 1,
})

render() {
const { log, logCount, open } = this.state
const btnContent = open ? 'Close Portal' : 'Open Portal'

return (
<div>
<div>
<Button content={btnContent} onClick={this.handleClick.bind(this, btnContent)} />
<Divider />
<Button size="small" onClick={this.clearLog} content="Clear" />
<span>
Event Log <Label circular>{logCount}</Label>
</span>
<pre>{log.map((e, i) => <div key={i}>{e}</div>)}</pre>
</div>
<Portal open={open}>
<div
style={{
background: '#ddd',
position: 'fixed',
left: '40%',
top: '45%',
zIndex: 1000,
}}
>
<Header>This is a controlled portal</Header>
<p>Portals have tons of great callback functions to hook into.</p>
<p>To close, simply click the close button</p>
</div>
</Portal>
</div>
)
}
}

export default PortalExampleControlled
21 changes: 21 additions & 0 deletions docs/src/examples/components/Portal/Types/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react'

import ComponentExample from 'docs/src/components/ComponentDoc/ComponentExample'
import ExampleSection from 'docs/src/components/ComponentDoc/ExampleSection'

const PortalTypesExamples = () => (
<ExampleSection title="Types">
<ComponentExample
title="Basic"
description="A basic portal."
examplePath="components/Portal/Types/PortalExample"
/>
<ComponentExample
title="Controlled"
description="A controlled portal."
examplePath="components/Portal/Types/PortalExampleControlled"
/>
</ExampleSection>
)

export default PortalTypesExamples
10 changes: 10 additions & 0 deletions docs/src/examples/components/Portal/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react'
import Types from './Types'

const PortalExamples = () => (
<div>
<Types />
</div>
)

export default PortalExamples
Loading