Skip to content

Commit

Permalink
Add platform component (#18058)
Browse files Browse the repository at this point in the history
* Add platform component

* Improve platform implementation in RN.

* Add more documentation and tests.

* Update readme file.

* Update tests.

* Fix filenames for native versions.

* Add license attribution

* Remove unnecessary lines.

* Improve documentation

* Remove trailing space

* Update packages/element/src/platform.js

Co-Authored-By: Grzegorz (Greg) Ziółkowski <[email protected]>

* Update readme.

* Fix lint error.
  • Loading branch information
SergioEstevao authored and hypest committed Nov 4, 2019
1 parent ef6ebbd commit 051ac02
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/element/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,30 @@ _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
import { Platform } from '@wordpress/element';

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

<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';
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;
32 changes: 32 additions & 0 deletions packages/element/src/platform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 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 = {
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
* import { Platform } from '@wordpress/element';
*
* const placeholderLabel = Platform.select( {
* native: __( 'Add media' ),
* web: __( 'Drag images, upload new ones or select files from your library.' ),
* } );
* ```
*/
export default Platform;
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' );
} );
} );

0 comments on commit 051ac02

Please sign in to comment.