Skip to content

Commit

Permalink
docs: update docs and more comprehensive e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdyman committed Apr 1, 2023
1 parent 267ff68 commit 84a15fb
Show file tree
Hide file tree
Showing 31 changed files with 1,316 additions and 584 deletions.
10 changes: 7 additions & 3 deletions docs/storybook/.storybook/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import path from 'path';
const path = require('path');

import codesandbox from 'remark-codesandbox';
const codesandbox = require('remark-codesandbox');

const config = {
core: {
builder: 'webpack5',
},

typescript: {
reactDocgen: 'react-docgen-typescript-plugin',
},

addons: [
'@storybook/addon-essentials',
{
Expand Down Expand Up @@ -64,4 +68,4 @@ const config = {
},
};

export default config;
module.exports = config;
22 changes: 18 additions & 4 deletions docs/storybook/.storybook/preview-head.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<style type="text/css">
:root {
--rcs-brand-primary: rgb(200, 109, 252);
--rcs-brand-alpha: rgba(89, 47, 114, 0.25);
--sb-brand-outline-base: rgba(255, 255, 255, 0.1);
--sb-brand-contrast: #ffffff;
--sb-surface-top-base: rgb(39, 37, 39, 0.5);
Expand Down Expand Up @@ -55,6 +57,20 @@
.docblock-argstable-body code,
.sbdocs.sbdocs-p code {
border-color: var(--sb-brand-outline-base) !important;
color: var(--sb-brand-contrast) !important;
}

blockquote.sbdocs.sbdocs-blockquote, .sb-custom-info {
border-left: 3px solid var(--rcs-brand-alpha) !important;
border-left-color: var(--rcs-brand-primary) !important;
background-color: var(--rcs-brand-alpha) !important;
padding: 0.5rem 0.75rem !important;
border-radius: 0.2rem !important;
}

.sb-custom-info > * {
padding: 0 !important;
margin: 0 !important;
}

.sb-show-main > #root {
Expand All @@ -77,10 +93,8 @@
.sb-custom-note {
display: block;
font-family: sans-serif;
margin-bottom: 0.5rem;
padding: 0.5rem;
border-radius: 0.25rem;
background-color: rgba(0, 0, 0, 0.85);
padding: 0.75rem;
background-color: #1b151f;
color: white;
}

Expand Down
4 changes: 2 additions & 2 deletions docs/storybook/.storybook/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { Theme, themes } from '@storybook/theming';
export const theme: Partial<Theme> = {
...themes.dark,
appBorderRadius: 3,
colorSecondary: '#b464fa',
barSelectedColor: '#b464fa',
colorSecondary: '#c86dfc',
barSelectedColor: '#c86dfc',
};
25 changes: 16 additions & 9 deletions docs/storybook/content/00-intro.stories.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Meta } from '@storybook/addon-docs';

<Meta title="Docs/Intro" />
<Meta title="Docs/Introduction" />

<div style={{ maxWidth: '42rem', margin: '0 auto' }}>

Expand All @@ -15,26 +15,26 @@ import { Meta } from '@storybook/addon-docs';
<br />

<div style={{ display: 'flex', gap: '0.5rem', justifyContent: 'center', marginBottom: '0.5rem' }}>
<a href="https://github.com/nerdyman/react-compare-slider/blob/master/LICENSE">
<img src="https://img.shields.io/npm/l/react-compare-slider.svg" alt="License MIT" />
<a href="https://github.com/nerdyman/react-compare-slider/blob/main/LICENSE">
<img alt="License MIT" src="https://img.shields.io/npm/l/react-compare-slider.svg" />
</a>
<a href="https://npmjs.com/package/react-compare-slider">
<img src="https://img.shields.io/npm/v/react-compare-slider.svg" alt="NPM package" />
<img alt="npm version" src="https://img.shields.io/npm/v/react-compare-slider.svg" />
</a>
<a href="https://bundlephobia.com/result?p=react-compare-slider">
<img src="https://img.shields.io/bundlephobia/minzip/react-compare-slider.svg" alt="Bundle size" />
<img alt="Bundle size" src="https://img.shields.io/bundlephobia/minzip/react-compare-slider.svg" />
</a>
</div>

<div style={{ display: 'flex', gap: '0.5rem', justifyContent: 'center' }}>
<a href="https://github.com/nerdyman/react-compare-slider/actions?query=workflow%3Abuild">
<img src="https://github.com/nerdyman/react-compare-slider/actions/workflows/ci.yml/badge.svg" alt="Build Status" />
<img alt="GitHub CI status" src="https://img.shields.io/github/actions/workflow/status/nerdyman/react-compare-slider/ci.yml" />
</a>
<a href="https://codeclimate.com/github/nerdyman/react-compare-slider">
<img src="https://img.shields.io/codeclimate/coverage/nerdyman/react-compare-slider" alt="Coverage" />
<a href="https://codecov.io/github/nerdyman/react-compare-slider" >
<img alt="Code coverage" src="https://img.shields.io/codecov/c/github/nerdyman/react-compare-slider?token=yhvFTuC7bh" />
</a>
<a href="https://github.com/nerdyman/react-compare-slider">
<img src="https://img.shields.io/badge/🐙-source-blue.svg" alt="Demos" />
<img alt="Source code" src="https://img.shields.io/badge/🐙-source-blue.svg" />
</a>
</div>

Expand Down Expand Up @@ -65,6 +65,13 @@ Storybook demos are within iframes which can sometimes cause the slider position
meet the edges of the container when sliding quickly - this is a browser limitation and
only occurs when the slider is within an iframe.

## Real World Examples

- [Official GOV.UK Coronavirus Dashboard](https://coronavirus.data.gov.uk/details/interactive-map/vaccinations#vaccinations-map-container)
- [RestorePhotos.io](https://www.restorephotos.io/restore)
- [Upscayl, Free and Open Source AI Image Upscaler](https://github.com/upscayl/upscayl#free-and-open-source-ai-image-upscaler)
- [DevTools-X, A collection of offline first developer utilities](https://github.com/fosslife/devtools-x)

---

<a target="_blank" rel="noopener noreferrer" style={{ display: 'block', margin: '0 auto', textAlign: 'center' }} href="https://github.com/nerdyman/react-compare-slider">View on GitHub</a>
Expand Down
17 changes: 13 additions & 4 deletions docs/storybook/content/01-api.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {

<Description of={ReactCompareSlider} />

[🔗 Source](https://github.com/nerdyman/react-compare-slider/blob/master/src/ReactCompareSlider.tsx)
[🔗 Source](https://github.com/nerdyman/react-compare-slider/blob/main/src/ReactCompareSlider.tsx)

### Props

Expand All @@ -28,7 +28,7 @@ import {

<Description of={ReactCompareSliderHandle} />

[🔗 Source](https://github.com/nerdyman/react-compare-slider/blob/master/src/ReactCompareSliderHandle.tsx)
[🔗 Source](https://github.com/nerdyman/react-compare-slider/blob/main/src/ReactCompareSliderHandle.tsx)

### Props

Expand All @@ -39,7 +39,7 @@ import {

<Description of={ReactCompareSliderImage} />

[🔗 Source](https://github.com/nerdyman/react-compare-slider/blob/master/src/ReactCompareSliderImage.tsx)
[🔗 Source](https://github.com/nerdyman/react-compare-slider/blob/main/src/ReactCompareSliderImage.tsx)

### Props

Expand All @@ -50,7 +50,7 @@ import {

<Description of={styleFitContainer} />

[🔗 Source](https://github.com/nerdyman/react-compare-slider/blob/master/src/utils.ts#L7)
[🔗 Source](https://github.com/nerdyman/react-compare-slider/blob/main/src/utils.ts#L16)

The `styleFitContainer` utility makes any [replaced element](https://developer.mozilla.org/en-US/docs/Web/CSS/Replaced_element)
fill its parent while maintaining the correct aspect ratio.
Expand All @@ -76,3 +76,12 @@ import { styleFitContainer } from 'react-compare-slider';
/>
</div>
```

## `useReactCompareSliderRef`

[🔗 Source](https://github.com/nerdyman/react-compare-slider/blob/main/src/useReactCompareSliderRef.ts)

The `useReactCompareSliderRef` provides a ref to the root element of the slider and exposes the internal
function used to control the position of the slider. It offers performant programmatic control of the slider.

Check out the [`useReactCompareSliderRef` docs](/story/docs-usereactcomparesliderref--page) more information.
2 changes: 1 addition & 1 deletion docs/storybook/content/02-images.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ import { ReactCompareSliderImage } from 'react-compare-slider';

## Live Examples

Checkout the [Images demos](?path=/story/demos--images) to experiment with images.
Checkout the [Images examples](/story/demos--images).
32 changes: 15 additions & 17 deletions docs/storybook/content/03-handles.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import { ReactCompareSliderHandle } from 'react-compare-slider';

# Using Handles

<Canvas>
<Story id="demos-handles--individual-styles" />
</Canvas>
<p class="sb-custom-info">

**Note**: You should use a non-interactive elements (`div`, `span`, etc.) for custom `handle`
components as handles are rendered within a `button` by the slider.

</p>

Custom Handles can be used via the `handle` prop on the main slider component.
If a custom `handle` is not supplied `ReactCompareSliderHandle` will
Expand All @@ -25,9 +28,9 @@ customize it to fit your requirements.

#### CSS `className`

- `__rcs-handle-root` - The root element.
- `__rcs-handle-button` - The circular element shown in the middle.
- `__rcs-handle-line` - For each line either side of the button.
- `.__rcs-handle-root` - The root element.
- `.__rcs-handle-button` - The circular element shown in the middle.
- `.__rcs-handle-line` - For each line either side of the button.

<br />

Expand All @@ -39,25 +42,20 @@ customize it to fit your requirements.

The colors used in `ReactCompareSliderHandle` are inherited from the root element's `color`
property using the `currentColor` keyword (except for `boxShadow`).
To set all colors in sync just change the `style` property or use the `__rcs-handle-root` class.
To set all colors in sync just change the `style` property or use the `.__rcs-handle-root` class.

<Canvas>
<Story id="demos-handles--inherited-color" />
<Story id="handles--inherited-color" />
</Canvas>

### Custom Standalone Handle Usage

```jsx
import { ReactCompareSlider } from 'react-compare-slider';

/** Render a completely custom handle. */
const MyCustomHandle = () => (
<div style={{ width: '5px', height: '100%', backgroundColor: red }}></div>;
)
<Canvas>
<Story id="handles-customcomponent--custom-component" />
</Canvas>

<ReactCompareSlider handle={<MyCustomHandle />} />
```

### Live Examples

Check out the [handles demos](/?path=/story/demos-handles--inherited-color) to experiment with handles.
Check out the [handles examples](/story/handles--inherited-color).
8 changes: 4 additions & 4 deletions docs/storybook/content/05-only-handle-draggable.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ container to the handle.
where the user needs to touch the slider container to scroll past it
without the slider container capturing the event, changing its position.
`onlyHandleDraggable` is also useful when comparing interactive components which
have their own pointer events, such as [embedded maps](/?path=/story/demos-custom-components--google-maps).
have their own pointer events, such as [embedded maps](/story/demos--google-maps).

<br/>

Expand All @@ -33,11 +33,11 @@ const isTouchDevice = window.matchMedia('(pointer: coarse)').matches;
</Canvas>

<Canvas>
<Story id="demos-custom-components--google-maps" />
<Story id="demos--google-maps" />
</Canvas>

<ArgsTable of={ReactCompareSlider} />

Also checkout the standalone
[Only Handle Draggable](?path=/story/demos--only-handle-draggable) and
[Google Maps](?path=/story/demos-custom-components--google-maps) demos.
[Only Handle Draggable](/story/demos--only-handle-draggable) and
[Google Maps](/story/recipies-googlemaps--google-maps) demos.
2 changes: 1 addition & 1 deletion docs/storybook/content/06-bounds-padding.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ boundsPadding={80}

## Live Examples

Checkout the [Bounds padding demos](?path=/story/demos--bounds-padding).
Checkout the [Bounds padding examples](/story/demos--bounds-padding).
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ of the mouse/pointer while the slider is being hovered over.

## Live Examples

Checkout the [Change Position on Hover demos](?path=/story/demos--change-position-on-hover).
Checkout the [Change Position on Hover examples](/story/demos--change-position-on-hover).

40 changes: 40 additions & 0 deletions docs/storybook/content/08-keyboard-increment.stories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs';

import { ReactCompareSlider, ReactCompareSliderImage } from 'react-compare-slider';

<Meta title="Docs/Keyboard Increment" component={ReactCompareSlider} />

# Using `keyboardIncrement`

The `keyboardIncrement` prop allows you to use the keyboard arrows to increment/decrement the slider
position when the slider `handle` is focused.

The increment can be either a `number` which evaluates to a pixel value or a `string` value
ending in `%`, e.g. `20%` to move the slider 20% of the total slider width in landscape mode
or height in portrait mode. A percentage is usually preferable as it adapts relatively to the width or
height of the slider, making it behaving consistently across resolutions.

```jsx
// Increment by 20 pixels.
<ReactCompareSlider
keyboardIncrement={20}
/>

// Increment by 20 percent of the slider width or height.
<ReactCompareSlider
keyboardIncrement={'20%'}
/>
```

<Canvas>
<Story id="demos--keyboard-increment" />
</Canvas>

<ArgsTable of={ReactCompareSlider} />

<br />

## Live Examples

Checkout the [Keyboard Increment examples](/story/demos--keyboard-increment).

62 changes: 62 additions & 0 deletions docs/storybook/content/09-use-react-compare-slider-ref.stories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs';

import { ReactCompareSlider, ReactCompareSliderImage, UseReactCompareSliderReturn } from 'react-compare-slider';

<Meta title="Docs/useReactCompareSliderRef" component={ReactCompareSlider} />

# Using the `useReactCompareSliderRef` Hook

<p class="sb-custom-info">

**Note**: Properties returned from the hook are only usable _after_ the component has mounted.

</p>

The `useReactCompareSliderRef` hook allows you to access the root container as a ref (`rootContainer`) and provides
access to the internal function used to performantly update the slider position (`setPosition`).

| Property | Type | Description |
| :-- | :-- | :-- |
| `rootContainer` | `HTMLDivElement` or `null` | The root container DOM element. |
| `setPosition` | `(position: number) => void` | Set the slider position to the given percentage. |

`setPosition` offers performant programmatic control of the slider. It is more performant because
it does not trigger a re-render, as opposed to setting the `position` prop which will re-render the
component then call the internal set position function as a side effect.

Another benefit of `setPosition` is that it can be used to reset the slider position back to
the initial position. This is not possible with the `position` prop.

```tsx
const Example = () => {
const reactCompareSliderRef = useReactCompareSliderRef();

// Safely use the ref properties in an effect or event handler callback.
useEffect(() => {
console.log(reactCompareSliderRef.current.rootContainer); // The root container DOM element.
reactCompareSliderRef.current.setPosition(20); // Set the slider position to 20%.
}, [reactCompareSliderRef]);

return (
<ReactCompareSlider
ref={reactCompareSliderRef}
/>
);
}

```

There's no limitation to `setPosition` usage, you can even use it to sync sliders together!

<Canvas>
<Story id="demos--use-react-compare-slider-ref" />
</Canvas>

<ArgsTable of={ReactCompareSlider} />

<br />

## Live Examples

Checkout the [useReactCompareSliderRef examples](/story/demos--use-react-compare-slider-ref).

0 comments on commit 84a15fb

Please sign in to comment.