-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Add platform component #18058
Add platform component #18058
Changes from 10 commits
70f00eb
49b17b2
7b600e9
fda2f16
ff8669f
75e4cf2
ae33dfe
3e72e5c
8b44e65
a23aa9f
e1841ff
77683ce
bd0883c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './react'; | ||
export * from './react-platform'; | ||
export * from './utils'; | ||
export { default as Platform } from './platform'; | ||
export { default as renderToString } from './serialize'; | ||
export { default as RawHTML } from './raw-html'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { Platform as OriginalPlatform } from 'react-native'; | ||
|
||
const Platform = { | ||
...OriginalPlatform, | ||
select: ( spec ) => { | ||
if ( 'android' in spec ) { | ||
return spec.android; | ||
} else if ( 'native' in spec ) { | ||
return spec.native; | ||
} | ||
return spec.default; | ||
}, | ||
}; | ||
|
||
export default Platform; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { Platform as OriginalPlatform } from 'react-native'; | ||
|
||
const Platform = { | ||
...OriginalPlatform, | ||
select: ( spec ) => { | ||
if ( 'ios' in spec ) { | ||
return spec.ios; | ||
} else if ( 'native' in spec ) { | ||
return spec.native; | ||
} | ||
return spec.default; | ||
}, | ||
}; | ||
|
||
export default Platform; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Parts of this source were derived and modified from react-native-web, | ||
* released under the MIT license. | ||
* | ||
* Copyright (c) 2016-present, Nicolas Gallagher. | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* | ||
*/ | ||
const Platform = { | ||
SergioEstevao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
OS: 'web', | ||
select: ( spec ) => ( 'web' in spec ? spec.web : spec.default ), | ||
}; | ||
/** | ||
* Component used to detect the current Platform being used. | ||
* Use Platform.OS === 'web' to detect if running on web enviroment. | ||
* | ||
* This is the same concept as the React Native implementation. | ||
* | ||
* @see https://facebook.github.io/react-native/docs/platform-specific-code#platform-module | ||
* | ||
* Here is an example of how to use the select method: | ||
* @example | ||
* ```js | ||
* const placeholderLabel = Platform.select( { | ||
SergioEstevao marked this conversation as resolved.
Show resolved
Hide resolved
SergioEstevao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* native: __( 'Add media' ), | ||
* web: __( 'Drag images, upload new ones or select files from your library.' ), | ||
* } ); | ||
* ``` | ||
*/ | ||
export default Platform; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be great to include an example in the docs as well, since this implementation differs a bit, right? Regardless, it can change upstream, so we should document it. Can we add simple unit tests that would also act like documentation? I personally, often look at unit tests when trying to learn some 3rd party software. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gziolo just added both, give it a look to see if it's ok now :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW, I sent a PR upstream I was going to suggest we could get the unit tests from there, but you added them faster 😅 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { shallow } from 'enzyme'; | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import Platform from '../platform'; | ||
|
||
describe( 'Platform', () => { | ||
it( 'is chooses the right thing', () => { | ||
const element = Platform.select( { | ||
web: shallow( <div></div> ), | ||
native: shallow( <button></button> ), | ||
} ); | ||
|
||
expect( element.type() ).toBe( 'div' ); | ||
} ); | ||
} ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { shallow } from 'enzyme'; | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import Platform from '../platform'; | ||
|
||
describe( 'Platform', () => { | ||
it( 'is chooses the right thing', () => { | ||
const element = Platform.select( { | ||
web: shallow( <div></div> ), | ||
native: shallow( <button></button> ), | ||
} ); | ||
|
||
expect( element.type() ).toBe( 'button' ); | ||
} ); | ||
} ); |
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.
This should be wrapped in a code block, it's currently not rendering well: https://github.com/WordPress/gutenberg/blob/3e72e5c719db4e0641b4ddbc5d1ab5f9145e64a1/packages/element/README.md#Platform