-
Notifications
You must be signed in to change notification settings - Fork 4k
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
feat(Responsive): add component #1872
Merged
Merged
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8c56cb9
feat(Breakpoint): add component
c419b05
feat(Breakpoint): add component
1a52e98
Merge branch 'master' of https://github.com/Semantic-Org/Semantic-UI-…
layershifter 8e3209c
feat(Breakpoint): add component
layershifter a3c6df1
Merge branch 'master' of https://github.com/Semantic-Org/Semantic-UI-…
7f072dc
Merge branch 'feat/breakpoint' of https://github.com/Semantic-Org/Sem…
3e90730
style(Breakpoint): prefix bools with "is"
3247632
refactor(Breakpoint): rename Responsive
levithomason c8be674
Merge branch 'master' of https://github.com/Semantic-Org/Semantic-UI-…
6d63ded
style(mixed): fix lint issues
aad879e
Merge branch 'feat/breakpoint' of https://github.com/Semantic-Org/Sem…
c9141fc
Merge branch 'master' of https://github.com/Semantic-Org/Semantic-UI-…
8187235
feat(Responsive): refactor component
8474bba
revert(customPropTypes): reverned unnecessary changes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
docs/app/Examples/addons/Breakpoint/Types/BreakpointExampleBreakpoint.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React from 'react' | ||
import { Breakpoint, Segment } from 'semantic-ui-react' | ||
|
||
const BreakpointExampleBreakpoint = () => ( | ||
<Segment.Group> | ||
<Breakpoint as={Segment} only='mobile'>Mobile</Breakpoint> | ||
<Breakpoint as={Segment} only='tablet'>Tablet</Breakpoint> | ||
<Breakpoint as={Segment} only='computer'>Computer</Breakpoint> | ||
<Breakpoint as={Segment} only='large screen'>Large Screen</Breakpoint> | ||
<Breakpoint as={Segment} only='widescreen'>Widescreen</Breakpoint> | ||
</Segment.Group> | ||
) | ||
|
||
export default BreakpointExampleBreakpoint |
38 changes: 38 additions & 0 deletions
38
docs/app/Examples/addons/Breakpoint/Types/BreakpointExampleContent.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React, { Component } from 'react' | ||
import { Breakpoint, Button, Menu } from 'semantic-ui-react' | ||
|
||
export default class BreakpointExampleContent extends Component { | ||
state = { active: 'home' } | ||
|
||
handleItemClick = (e, { name }) => this.setState({ active: name }) | ||
|
||
render() { | ||
const { active } = this.state | ||
|
||
return ( | ||
<Menu> | ||
<Menu.Item active={active === 'home'} content='Home' name='home' onClick={this.handleItemClick} /> | ||
<Menu.Item active={active === 'messages'} content='Messages' name='messages' onClick={this.handleItemClick} /> | ||
|
||
<Menu.Menu position='right'> | ||
<Menu.Item> | ||
<Breakpoint | ||
as={Button} | ||
content='Switch to desktop version' | ||
icon='desktop' | ||
labelPosition='left' | ||
only='mobile' | ||
/> | ||
<Breakpoint | ||
as={Button} | ||
content='Switch to mobile version' | ||
icon='mobile' | ||
labelPosition='left' | ||
only='computer tablet' | ||
/> | ||
</Menu.Item> | ||
</Menu.Menu> | ||
</Menu> | ||
) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
docs/app/Examples/addons/Breakpoint/Types/BreakpointExampleMultiple.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from 'react' | ||
import { Breakpoint, Segment } from 'semantic-ui-react' | ||
|
||
const BreakpointExampleMultiple = () => ( | ||
<Segment.Group> | ||
<Breakpoint as={Segment} only='mobile tablet'>Mobile & Tablet</Breakpoint> | ||
<Breakpoint as={Segment} only='computer tablet'>Tablet & Computer</Breakpoint> | ||
</Segment.Group> | ||
) | ||
|
||
export default BreakpointExampleMultiple |
27 changes: 27 additions & 0 deletions
27
docs/app/Examples/addons/Breakpoint/Types/BreakpointExampleNested.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from 'react' | ||
import { Breakpoint, Segment } from 'semantic-ui-react' | ||
|
||
const BreakpointExampleNested = () => ( | ||
<Segment.Group> | ||
<Breakpoint as={Segment} only='mobile tablet'> | ||
<Breakpoint only='mobile'> | ||
<p>Mobile</p> | ||
</Breakpoint> | ||
<Breakpoint only='tablet'> | ||
<p>Mobile</p> | ||
</Breakpoint> | ||
</Breakpoint> | ||
|
||
<Breakpoint as={Segment} only='computer'> | ||
<p>Computer</p> | ||
<Breakpoint only='large screen'> | ||
<p>Large Screen</p> | ||
</Breakpoint> | ||
<Breakpoint only='widescreen'> | ||
<p>Widescreen</p> | ||
</Breakpoint> | ||
</Breakpoint> | ||
</Segment.Group> | ||
) | ||
|
||
export default BreakpointExampleNested |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React from 'react' | ||
import { Message } from 'semantic-ui-react' | ||
|
||
import ComponentExample from 'docs/app/Components/ComponentDoc/ComponentExample' | ||
import ExampleSection from 'docs/app/Components/ComponentDoc/ExampleSection' | ||
|
||
const BreakpointTypesExamples = () => ( | ||
<ExampleSection title='Types'> | ||
<ComponentExample | ||
title='Breakpoint' | ||
description='A Breakpoint can appear only for a specific device or screen sizes.' | ||
examplePath='addons/Breakpoint/Types/BreakpointExampleBreakpoint' | ||
> | ||
<Message info> | ||
Instead of <code>Grid</code> visibility breakpoints, <code>Breakpoint</code> doesn't render invisible content. | ||
</Message> | ||
</ComponentExample> | ||
<ComponentExample | ||
title='Multiple' | ||
description='A Breakpoint can accept multiple sizes.' | ||
examplePath='addons/Breakpoint/Types/BreakpointExampleMultiple' | ||
/> | ||
<ComponentExample | ||
title='Content' | ||
description='A Breakpoint can contain a different content.' | ||
examplePath='addons/Breakpoint/Types/BreakpointExampleContent' | ||
/> | ||
<ComponentExample | ||
title='Nested' | ||
description='A Breakpoint can be nested.' | ||
examplePath='addons/Breakpoint/Types/BreakpointExampleNested' | ||
/> | ||
</ExampleSection> | ||
) | ||
|
||
export default BreakpointTypesExamples |
22 changes: 22 additions & 0 deletions
22
docs/app/Examples/addons/Breakpoint/Usage/BreakpointExamplePoints.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React from 'react' | ||
import { Breakpoint, Segment } from 'semantic-ui-react' | ||
|
||
const points = { | ||
computer: 990, | ||
largeScreen: 1300, | ||
mobile: 300, | ||
tablet: 800, | ||
widescreen: 1900, | ||
} | ||
|
||
const BreakpointExamplePoints = () => ( | ||
<Segment.Group> | ||
<Breakpoint as={Segment} only='mobile' points={points}>Mobile</Breakpoint> | ||
<Breakpoint as={Segment} only='tablet' points={points}>Tablet</Breakpoint> | ||
<Breakpoint as={Segment} only='computer' points={points}>Computer</Breakpoint> | ||
<Breakpoint as={Segment} only='large screen' points={points}>Large Screen</Breakpoint> | ||
<Breakpoint as={Segment} only='widescreen' points={points}>Widescreen</Breakpoint> | ||
</Segment.Group> | ||
) | ||
|
||
export default BreakpointExamplePoints |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from 'react' | ||
|
||
import ComponentExample from 'docs/app/Components/ComponentDoc/ComponentExample' | ||
import ExampleSection from 'docs/app/Components/ComponentDoc/ExampleSection' | ||
|
||
const BreakpointUsageExamples = () => ( | ||
<ExampleSection title='Usage'> | ||
<ComponentExample | ||
title='Points' | ||
description='You can define your own points.' | ||
examplePath='addons/Breakpoint/Usage/BreakpointExamplePoints' | ||
/> | ||
</ExampleSection> | ||
) | ||
|
||
export default BreakpointUsageExamples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from 'react' | ||
|
||
import Types from './Types' | ||
import Usage from './Usage' | ||
|
||
const BreakpointExamples = () => ( | ||
<div> | ||
<Types /> | ||
<Usage /> | ||
</div> | ||
) | ||
|
||
export default BreakpointExamples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import * as React from 'react'; | ||
import { GridOnlyProp } from '../../collections/Grid/GridColumn'; | ||
|
||
export interface BreakpointProps { | ||
[key: string]: any; | ||
|
||
/** An element type to render as (string or function). */ | ||
as?: any; | ||
|
||
/** Primary content. */ | ||
children?: React.ReactNode; | ||
|
||
/** A row can appear only for a specific device, or screen sizes. */ | ||
only: GridOnlyProp; | ||
|
||
/** | ||
* Called on update. | ||
* | ||
* @param {SyntheticEvent} event - The React SyntheticEvent object | ||
* @param {object} data - All props and the event value. | ||
*/ | ||
onUpdate?: BreakpointOnUpdateData; | ||
|
||
/** Breakpoints definition. */ | ||
points?: BreakpointPoints; | ||
} | ||
|
||
export interface BreakpointPoints { | ||
computer: number; | ||
largeScreen: number; | ||
mobile: number; | ||
tablet: number; | ||
widescreen: number; | ||
} | ||
|
||
export interface BreakpointOnUpdateData extends BreakpointProps { | ||
number: string; | ||
} | ||
|
||
declare const Breakpoint: React.ComponentClass<BreakpointProps>; | ||
|
||
export default Breakpoint; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import _ from 'lodash' | ||
import PropTypes from 'prop-types' | ||
import React, { Component } from 'react' | ||
|
||
import { | ||
customPropTypes, | ||
getElementType, | ||
getUnhandledProps, | ||
META, | ||
SUI, | ||
} from '../../lib' | ||
|
||
/** | ||
* A Breakpoint can control visibility of content. | ||
*/ | ||
export default class Breakpoint extends Component { | ||
static propTypes = { | ||
/** An element type to render as (string or function). */ | ||
as: customPropTypes.as, | ||
|
||
/** Primary content. */ | ||
children: PropTypes.node, | ||
|
||
/** A row can appear only for a specific device, or screen sizes. */ | ||
only: customPropTypes.onlyProp(SUI.VISIBILITY).isRequired, | ||
|
||
/** | ||
* Called on update. | ||
* | ||
* @param {SyntheticEvent} event - The React SyntheticEvent object | ||
* @param {object} data - All props and the event value. | ||
*/ | ||
onUpdate: PropTypes.func, | ||
|
||
/** Breakpoints definition. */ | ||
points: PropTypes.shape({ | ||
computer: PropTypes.number.isRequired, | ||
largeScreen: PropTypes.number.isRequired, | ||
mobile: PropTypes.number.isRequired, | ||
tablet: PropTypes.number.isRequired, | ||
widescreen: PropTypes.number.isRequired, | ||
}), | ||
} | ||
|
||
static defaultProps = { | ||
points: { | ||
computer: 992, | ||
largeScreen: 1200, | ||
mobile: 320, | ||
tablet: 768, | ||
widescreen: 1920, | ||
}, | ||
} | ||
|
||
static _meta = { | ||
name: 'Breakpoint', | ||
type: META.TYPES.ADDON, | ||
} | ||
|
||
constructor(...args) { | ||
super(...args) | ||
this.state = { width: window.innerWidth } | ||
} | ||
|
||
componentDidMount() { | ||
window.addEventListener('resize', this.handleUpdate) | ||
} | ||
|
||
componentWillUnmount() { | ||
window.removeEventListener('resize', this.handleUpdate) | ||
} | ||
|
||
// ---------------------------------------- | ||
// Breakpoint matchers | ||
// ---------------------------------------- | ||
|
||
isComputer = () => { | ||
const { points: { computer } } = this.props | ||
const { width } = this.state | ||
|
||
return width >= computer | ||
} | ||
|
||
isLargeScreen = () => { | ||
const { points: { largeScreen, widescreen } } = this.props | ||
const { width } = this.state | ||
|
||
return width >= largeScreen && width < widescreen | ||
} | ||
|
||
isMobile = () => { | ||
const { points: { mobile, tablet } } = this.props | ||
const { width } = this.state | ||
|
||
return width >= mobile && width < tablet | ||
} | ||
|
||
isTablet = () => { | ||
const { points: { computer, tablet } } = this.props | ||
const { width } = this.state | ||
|
||
return width >= tablet && width < computer | ||
} | ||
|
||
isWidescreen = () => { | ||
const { points: { widescreen } } = this.props | ||
const { width } = this.state | ||
|
||
return width >= widescreen | ||
} | ||
|
||
// ---------------------------------------- | ||
// Helpers | ||
// ---------------------------------------- | ||
|
||
isVisible = () => { | ||
const { only = '' } = this.props | ||
const points = only.replace('large screen', 'largeScreen').split(' ') | ||
|
||
return _.some(points, point => _.invoke(this, `is${_.upperFirst(point)}`)) | ||
} | ||
|
||
// ---------------------------------------- | ||
// Event handlers | ||
// ---------------------------------------- | ||
|
||
handleUpdate = e => { | ||
requestAnimationFrame(() => { | ||
const width = window.innerWidth | ||
|
||
this.setState({ width }) | ||
_.invoke(this.props, 'onUpdate', e, { ...this.props, width }) | ||
}) | ||
} | ||
|
||
// ---------------------------------------- | ||
// Render | ||
// ---------------------------------------- | ||
|
||
render() { | ||
const { children } = this.props | ||
|
||
const ElementType = getElementType(Breakpoint, this.props) | ||
const rest = getUnhandledProps(Breakpoint, this.props) | ||
|
||
if (this.isVisible()) return <ElementType {...rest}>{children}</ElementType> | ||
return null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default, BreakpointPoints, BreakpointProps, BreakpointOnUpdateData } from './Breakpoint'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sinon should be updated to support
value
: