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

Add platform component #18058

Merged
merged 13 commits into from
Oct 24, 2019
22 changes: 22 additions & 0 deletions packages/element/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,28 @@ _Related_

- <https://reactjs.org/docs/react-api.html#reactmemo>

<a name="Platform" href="#Platform">#</a> **Platform**

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.

_Related_

- <https://facebook.github.io/react-native/docs/platform-specific-code#platform-module>

Here is an example of how to use the select method:

_Usage_

```js
const placeholderLabel = Platform.select( {
native: __( 'Add media' ),
web: __( 'Drag images, upload new ones or select files from your library.' ),
} );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```

<a name="RawHTML" href="#RawHTML">#</a> **RawHTML**

Component used as equivalent of Fragment with unescaped HTML, in cases where
Expand Down
1 change: 1 addition & 0 deletions packages/element/src/index.js
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';
koke marked this conversation as resolved.
Show resolved Hide resolved
export { default as renderToString } from './serialize';
export { default as RawHTML } from './raw-html';
18 changes: 18 additions & 0 deletions packages/element/src/platform.android.js
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;
18 changes: 18 additions & 0 deletions packages/element/src/platform.ios.js
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;
30 changes: 30 additions & 0 deletions packages/element/src/platform.js
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;
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, I sent a PR upstream

facebook/react-native#26966

I was going to suggest we could get the unit tests from there, but you added them faster 😅

19 changes: 19 additions & 0 deletions packages/element/src/test/platform.js
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' );
} );
} );
19 changes: 19 additions & 0 deletions packages/element/src/test/platform.native.js
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' );
} );
} );