Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/anubhavsrivastava/react-for-electron-only

React component for rendering component for Electron based clients only
https://github.com/anubhavsrivastava/react-for-electron-only

desktop detect-electron electron electron-react isomorphic-applications react react-component react-electron reactjs

Last synced: 4 months ago
JSON representation

React component for rendering component for Electron based clients only

Awesome Lists containing this project

README

        

# `react-for-electron-only`

[![build status](https://api.travis-ci.org/anubhavsrivastava/react-for-electron-only.svg?branch=master)](https://travis-ci.org/anubhavsrivastava/react-for-electron-only/)
[![Coverage Status](https://coveralls.io/repos/github/anubhavsrivastava/react-for-electron-only/badge.svg?branch=master)](https://coveralls.io/github/anubhavsrivastava/react-for-electron-only?branch=master)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
[![GitHub issues](https://img.shields.io/github/issues/anubhavsrivastava/react-for-electron-only.svg?style=flat-square)](https://github.com/anubhavsrivastava/react-for-electron-only/issues)

[![NPM](https://nodei.co/npm/electron-only.png?downloads=true&stars=true)](https://www.npmjs.com/package/electron-only)

React component for rendering component for Electron based clients only. If you’re serving up the same code to a Web client and an Electron-based desktop client, this could help you split them apart where-ever necessary. It can be used for detection of [Electron](https://electronjs.org/) (ref: [isElectron](#iselectron-function) function).
`` would render children in case when react application runs in [Electron](https://electronjs.org/) Desktop client, else would render nothing(null) .

## Use Cases

### Trivial Case

Considering any isomorphic app which is distributed on ElectronJS and also over Web with same react codebase, say for eg. slack (which renders same UI on electron desktop client and web), there are use cases when you want to render particular DOM node/components on Electron Only and not on browser or vice versa.

### Exclusive Functionality

There are set of APIs from electron that would give native UI feel eg, [`dialog`](https://electronjs.org/docs/api/dialog) module.
For instance, one may have folder/file path as input text on browser while on electron one may use `dialog.showOpenDialog` functionality via a button.
Use of such APIs also eliminate some validation errors.

## Install

```
$ npm install electron-only --save
```

or

```
yarn add electron-only
```

## Example

1. ``

import ForElectronOnly from 'electron-only';

...
render(){
return (


This will only get rendered on Electron


)
}
...

This will render `div.electron-ui` on electron and nothing on normal browsers.

2. `` with `fallbackComponent` option

import ForElectronOnly from 'electron-only';

...
render(){
return (
This will only get rendered in Browser }>


This will only get rendered on Electron


)
}
...

This will render `div.electron-ui` on electron and `div.browser-ui` on browser.

3. `ForNonElectronOnly` (supports fallbackComponent prop)

import {ForNonElectronOnly} from 'electron-only';

...
render(){
return (
This will only get rendered in Electron }>


This will only get rendered on Browser


)
}
...

`ForNonElectronOnly` is exactly opposite of `ForElectronOnly`, ie,
it will render `div.electron-ui` on electron and `div.browser-ui` on browser.

## APIs

### `ForElectronOnly` Component

#### Import mechanism

import ForElectronOnly from 'electron-only'

#### Properties

| prop | type | description | default value |
| ------------------ | ------ | ----------------------------------------- | ------------- |
| children (default) | `node` | Component to be render for electron | `null` |
| fallbackComponent | `node` | Component/Node to be rendered for browser | `null` |

### `ForNonElectronOnly` Component

#### Import mechanism

import {ForNonElectronOnly} from 'electron-only'

#### Properties

| prop | type | description | default value |
| ------------------ | ------ | ------------------------------------------ | ------------- |
| children (default) | `node` | Component to be render for browser | `null` |
| fallbackComponent | `node` | Component/Node to be rendered for electron | `null` |

### `isElectron` function

#### Import mechanism

import {isElectron} from 'electron-only'
//or import ElectronOnly from 'electron-only';
// and then ElectronOnly.isElectron function can be used

#### Return values

| value | description | comment |
| ----- | ----------------------------------- | ----------------------------------------- |
| true | if code is executed on Electron | |
| false | if code is not executed on Electron | implies both browser and server execution |

---

## Contribution

Suggestions and PRs are welcome!

Please create issue or open PR request for contribution.

---

## License

[![Open Source Love](https://badges.frapsoft.com/os/mit/mit.svg?v=102)](LICENSE)

refer `LICENSE` file in this repository.