dha-breathing-exercise
This module is a React BreathingExercise component used for inhale/exhale breathing with a seconds timer and breathing cycles count.
Getting Started
Install
Install from npm:
- npm i dha-breathing-exercise dha-assets
Importing Your Own Local Image Files
If you want to use your own local image files with this package there are a couple of ways to do this in create-react-app, see Option 1 and Option 2.
Option 1: Import Image in JS Using Relative Path.
Importing Images in JS Example:
Disclaimer: This is a non-compilable example. For a compilable working example, see the full functional and class component examples below.
import imagePng from '../assets/images/image.png';
<Background alt="Image alt" image={imagePng} scale={1} styles={styles.background} />;
<Image alt="Image alt" maxWidth="250px" src={imagePng} styles={styles.image} />;
Option 2: CRA Public Folder Assets - NOT Recommended
Using CRA Public Folder Example:
Disclaimer: This is a non-compilable example. For a compilable working example, see the full functional and class component examples below.
<Background alt="Image alt" image="assets/images/image.png" scale={1} styles={styles.background} />;
<Image alt="Image alt" maxWidth="250px" src="assets/images/image.png" styles={styles.image} />;
BreathingExercise Component
BreathingExercise Props:
- breathingCycles: Number of breathing cycles
- exhaleSeconds: Exhale length in seconds
- inhaleSeconds: Inhale length in seconds
- redirectButtonRoute: Redirect button route
- redirectButtonText: Redirect button text
- alt: Background image text alternative
- image: Background image full path and file, or imported image file
Disclaimer: For this demo code to work with imported images in JS and from the public folder options, add and/or replace dha-logo.png with your own image file.
Add and/or Replace Image Files in Application:
- ./public/dha-logo.png
- ./src/assets/images/dha-logo.png
Home.tsx - Functional component
import { Typography } from '@material-ui/core';
import { BreathingExercise } from 'dha-breathing-exercise';
import React from 'react';
import dHALogoRelAssets from '../../assets/images/dha-logo.png';
const Home: React.FC = () => (
<>
<Typography variant="h5">Option 1: Import Image in JS Using Relative Path</Typography>
<BreathingExercise
breathingCycles={8}
exhaleSeconds={3}
inhaleSeconds={3}
redirectButtonRoute="#/"
redirectButtonText="Next"
alt="Background Image"
image={dHALogoRelAssets}
/>
<Typography variant="h5">Option 2: Hosted Image</Typography>
<BreathingExercise
breathingCycles={8}
exhaleSeconds={3}
inhaleSeconds={3}
redirectButtonRoute="#/"
redirectButtonText="Next"
alt="Background Image"
image="https://via.placeholder.com/250"
/>
<Typography variant="h5">Option 3: CRA Public Folder Image - NOT Recommended</Typography>
<BreathingExercise
breathingCycles={8}
exhaleSeconds={3}
inhaleSeconds={3}
redirectButtonRoute="#/"
redirectButtonText="Next"
alt="Background Image"
image="dha-logo.png"
/>
</>
);
export default Home;
Home.tsx - Class component
import { Typography } from '@material-ui/core';
import { BreathingExercise } from 'dha-breathing-exercise';
import React from 'react';
import dHALogoRelAssets from '../../assets/images/dha-logo.png';
class Home extends React.Component {
render() {
return (
<>
<Typography variant="h5">Option 1: Import Image in JS Using Relative Path</Typography>
<BreathingExercise
breathingCycles={8}
exhaleSeconds={3}
inhaleSeconds={3}
redirectButtonRoute="#/"
redirectButtonText="Next"
alt="Background Image"
image={dHALogoRelAssets}
/>
<Typography variant="h5">Option 2: Hosted Image</Typography>
<BreathingExercise
breathingCycles={8}
exhaleSeconds={3}
inhaleSeconds={3}
redirectButtonRoute="#/"
redirectButtonText="Next"
alt="Background Image"
image="https://via.placeholder.com/250"
/>
<Typography variant="h5">Option 3: CRA Public Folder Image - NOT Recommended</Typography>
<BreathingExercise
breathingCycles={8}
exhaleSeconds={3}
inhaleSeconds={3}
redirectButtonRoute="#/"
redirectButtonText="Next"
alt="Background Image"
image="dha-logo.png"
/>
</>
);
}
}
export default Home;
Contribute
Setup
Build
The package uses the Typescript compiler (TSC) to build the source code. To build the project run the following command:
Link peerDependencies
Note: You must tell the local dha-breathing-exercise to use all peerDependencies from the app you are including the dha-breathing-exercise into using npm link (the example below is for using the dha-breathing-exercise in the pwa-starter).
See the dha-breathing-exercise package.json peerDependencies section.
"peerDependencies": {
"@material-ui/core": "^X.X.X",
"@material-ui/icons": "^X.X.X",
"react": "^X.X.X",
"react-dom": "^X.X.X"
}
- npm link ../pwa-starter/node_modules/@material-ui/core
- npm link ../pwa-starter/node_modules/@material-ui/icons
- npm link ../pwa-starter/node_modules/@material-ui/styles
- This is not a listed peerDependency, but will throw a warning in the console that multiple instances of @material-ui/styles exists.
- npm link ../pwa-starter/node_modules/react
- npm link ../pwa-starter/node_modules/react-dom
- Repeat for any remaining peerDependencies that are not listed.
Install Local
You can install the package into your own React application/pwa-starter as a dependency. After you build the dha-breathing-exercise, install the package into the app using the following command:
- cd pwa-starter
- npm i /abs/or/rel/path/to/dha-breathing-exercise or npm link /abs/or/rel/path/to/dha-breathing-exercise
- ex. npm i ../dha-breathing-exercise or npm link ../dha-breathing-exercise
- pwa-starter and dha-breathing-exercise are sibling folders in the example. Adjust this to your project name and directory structure.
Troubleshooting
Npm Install Issues
- Clearing the package-lock.json and node_modules folder can help ensure that an app or package starts in a clean state.
- rm -rf package-lock.json node_modules
Hook Errors
- Hook errors can be caused when a local package is installed into an application where the package devDependencies conflict with the application dependencies by having multiple instances of the same dependency.
- You must tell the local dha-breathing-exercise to use peerDependencies from the app you are including the dha-breathing-exercise into using npm link (the example below is for using the dha-breathing-exercise in the pwa-starter).
- Refer to "Link peerDependencies" section above.
- npm link ../pwa-starter/node_modules/@material-ui/core
Test
- Tests can be ran with the
npm test
command. - Tests are ran using Jest with Enzyme - More information about getting started with Jest can be found here.
Example:
test('renders without crashing', () => {
shallow(<Demo prop1="1" prop2={2} />);
});
test('hello world string', () => {
expect(demo()).toMatch(`RENAME Demo files to start.`);
});
Source Code
https://bitbucket.org/wmtp/dha-breathing-exercise
NPM
https://www.npmjs.com/package/dha-breathing-exercise
License
This project is licensed under the MIT License.