Skip to content

Commit

Permalink
Add sensor documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
clauderic committed Aug 22, 2023
1 parent f14a5f6 commit cd485cc
Show file tree
Hide file tree
Showing 29 changed files with 355 additions and 61 deletions.
19 changes: 17 additions & 2 deletions apps/docs/.storybook/preview-head.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,22 @@
border-bottom: none !important;
}

p {
h3 {
font-size: 25px !important;
margin-top: 30px !important;
margin-bottom: 16px !important;
}

h4 {
font-size: 20px !important;
}

p, li {
font-size: 17px !important;
line-height: 2 !important;
line-height: 30px !important;
}


h1 + p {
font-size: 20px !important;
}
Expand All @@ -59,6 +70,10 @@
box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.10), inset 0 0 1px hsla(203, 50%, 30%, 0.5), inset 0 0 0 2px #4c9ffe;
}

a {
color: #3a58ed !important;
}

section {
display: flex;
flex-wrap: wrap;
Expand Down
2 changes: 1 addition & 1 deletion apps/docs/.storybook/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import {Unstyled} from '@storybook/blocks';

import {Code} from '../stories/react/components';
import {Code} from '../stories/components';

const preview = {
parameters: {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'prismjs/components/prism-jsx.min';

import copy from './copy.svg';
import styles from './CodeHighlighter.module.css';
import {classNames, createRange} from '../../../../../utilities';
import {classNames, createRange} from '../../../../utilities';

interface Props {
children: string;
Expand Down
File renamed without changes.
21 changes: 21 additions & 0 deletions apps/docs/stories/components/Info/Info.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.Info {
display: flex;
gap: 10px;
align-items: center;
justify-content: stretch;
padding: 16px 24px;
color: rgba(0,37,158, 0.9);
background-color: rgba(1,68,255,0.059);
border: 1px solid rgb(0 37 158 / 17%);
border-radius: 10px;
}

.Info > p {
color: inherit;
margin: 0;
}

.Info > svg {
width: 20px;
height: 20px;
}
29 changes: 29 additions & 0 deletions apps/docs/stories/components/Info/Info.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, {type PropsWithChildren} from 'react';

import styles from './Info.module.css';

const icon = (
<svg
width="15"
height="15"
viewBox="0 0 15 15"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M7.49991 0.876892C3.84222 0.876892 0.877075 3.84204 0.877075 7.49972C0.877075 11.1574 3.84222 14.1226 7.49991 14.1226C11.1576 14.1226 14.1227 11.1574 14.1227 7.49972C14.1227 3.84204 11.1576 0.876892 7.49991 0.876892ZM1.82707 7.49972C1.82707 4.36671 4.36689 1.82689 7.49991 1.82689C10.6329 1.82689 13.1727 4.36671 13.1727 7.49972C13.1727 10.6327 10.6329 13.1726 7.49991 13.1726C4.36689 13.1726 1.82707 10.6327 1.82707 7.49972ZM8.24992 4.49999C8.24992 4.9142 7.91413 5.24999 7.49992 5.24999C7.08571 5.24999 6.74992 4.9142 6.74992 4.49999C6.74992 4.08577 7.08571 3.74999 7.49992 3.74999C7.91413 3.74999 8.24992 4.08577 8.24992 4.49999ZM6.00003 5.99999H6.50003H7.50003C7.77618 5.99999 8.00003 6.22384 8.00003 6.49999V9.99999H8.50003H9.00003V11H8.50003H7.50003H6.50003H6.00003V9.99999H6.50003H7.00003V6.99999H6.50003H6.00003V5.99999Z"
fill="currentColor"
fillRule="evenodd"
clipRule="evenodd"
></path>
</svg>
);

export function Info({children}: PropsWithChildren) {
return (
<div className={styles.Info}>
{icon}
<p>{children}</p>
</div>
);
}
1 change: 1 addition & 0 deletions apps/docs/stories/components/Info/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {Info} from './Info';
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ import React from 'react';
import {Story, Unstyled} from '@storybook/blocks';
import {type StoryFn} from '@storybook/react';

import {classNames} from '../../../utilities';
import {classNames} from '../../utilities';
import {Code} from '../Code';
import styles from './Preview.module.css';

interface Props {
of?: StoryFn;
code?: string;
id?: string;
hero?: boolean;
tabs?: string[];
children?: React.ReactNode;
}

export function Preview({children, code, of, hero, tabs}: Props) {
export function Preview({children, code, of, hero, id, tabs}: Props) {
return (
<Unstyled>
<div className={classNames(styles.Preview, hero && styles.hero)}>
<div className={classNames(styles.Preview, hero && styles.hero)} id={id}>
{children ?? <Story of={of} />}
</div>
{code ? (
Expand Down
3 changes: 3 additions & 0 deletions apps/docs/stories/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export {Code} from './Code';
export {Info} from './Info';
export {Preview} from './Preview';
36 changes: 7 additions & 29 deletions apps/docs/stories/react/Draggable/DraggableExample.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import React, {PropsWithChildren, useState} from 'react';
import type {Modifiers, UniqueIdentifier} from '@dnd-kit/abstract';
import type {Modifiers, Sensors} from '@dnd-kit/abstract';
import {DragDropProvider, useDraggable} from '@dnd-kit/react';

import {Button} from '../components';
import {DraggableIcon} from '../icons';

interface Props {
container?: boolean;
container?: React.FC<PropsWithChildren<{}>> | string;
modifiers?: Modifiers;
sensors?: Sensors;
}

export function DraggableExample({container, modifiers}: Props) {
const Wrapper = container ? Container : 'div';
export function DraggableExample({container, modifiers, sensors}: Props) {
const Wrapper = container ?? 'div';

return (
<DragDropProvider>
<DragDropProvider sensors={sensors}>
<Wrapper>
<Draggable id="draggable" modifiers={modifiers} />
</Wrapper>
Expand All @@ -23,7 +24,7 @@ export function DraggableExample({container, modifiers}: Props) {
}

interface DraggableProps {
id: UniqueIdentifier;
id: string;
modifiers?: Modifiers;
}

Expand All @@ -42,26 +43,3 @@ function Draggable({id, modifiers}: DraggableProps) {
</Button>
);
}

function Container({children}: PropsWithChildren) {
return (
<div
style={{
display: 'flex',
width: '60%',
minWidth: 300,
margin: '40px 80px',
height: 350,
border: '1px solid',
alignItems: 'center',
justifyContent: 'center',
borderColor: 'rgba(0,0,0,0.2)',
padding: 30,
borderRadius: 8,
}}
data-container
>
{children}
</div>
);
}
33 changes: 31 additions & 2 deletions apps/docs/stories/react/Draggable/Modifiers/Modifiers.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import React, {type PropsWithChildren} from 'react';
import type {Meta, StoryObj} from '@storybook/react';
import {
RestrictToHorizontalAxis,
RestrictToVerticalAxis,
} from '@dnd-kit/abstract/modifiers';
import {RestrictToElement, RestrictToWindow} from '@dnd-kit/dom/modifiers';

import docs from './docs/ModifierDocs.mdx';
import {DraggableExample} from '../DraggableExample';

const meta: Meta<typeof DraggableExample> = {
title: 'React/Draggable/Modifiers',
component: DraggableExample,
tags: ['autodocs'],
parameters: {
docs: {
page: docs,
},
},
};

export default meta;
Expand Down Expand Up @@ -39,10 +47,31 @@ export const WindowModifier: Story = {
export const ContainerModifier: Story = {
name: 'Restrict to container',
args: {
container: true,
container({children}) {
return (
<div
style={{
display: 'flex',
width: '60%',
minWidth: 300,
margin: '40px 80px',
height: 350,
outline: '3px solid rgba(0,0,0,0.2)',
background: '#FFF',
alignItems: 'center',
justifyContent: 'center',
padding: 30,
borderRadius: 8,
}}
data-container
>
{children}
</div>
);
},
modifiers: [
RestrictToElement.configure({
getElement() {
element() {
return document.querySelector('[data-container]');
},
}),
Expand Down
68 changes: 68 additions & 0 deletions apps/docs/stories/react/Draggable/Modifiers/docs/ModifierDocs.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {RestrictToElement} from '@dnd-kit/dom/modifiers';

import {Info, Preview} from '../../../../components';
import {DraggableExample} from '../../DraggableExample';

# Modifiers

Modify or restrict the behavior of draggable elements.

<Preview id="modifier-hero" hero>
<DraggableExample
modifiers={[
RestrictToElement.configure({
element() {
return document.getElementById('modifier-hero');
},
}),
]}
/>
</Preview>

Modifiers let you dynamically modify the movement coordinates that are detected by sensors. They can be used for a wide range of use cases, for example:

- Restricting motion to a single axis
- Restricting motion to the draggable node container's bounding rectangle
- Restricting motion to the draggable node's scroll container bounding rectangle
- Applying resistance or clamping the motion

## Usage

Modifiers can be applied globally or to individual draggable elements.

### Global modifiers

Modifiers can be applied globally by passing them to the `<DragDropProvider>` component.

```jsx
import {DragDropProvider} from '@dnd-kit/react';
import {RestrictToVerticalAxis} from '@dnd-kit/abstract/modifiers';
import {RestrictToWindow} from '@dnd-kit/dom/modifiers';

function App() {
return (
<DragDropProvider modifiers={[RestrictToWindow, RestrictToVerticalAxis]}>
{...}
</DragDropProvider>
)
}
```

### Local modifiers

<Info>Local modifiers take precedence over global modifiers.</Info>

Modifiers can also be applied to individual draggable elements by passing them to the `modifiers` prop.

```jsx
import {useDraggable} from '@dnd-kit/react';
import {RestrictToVerticalAxis} from '@dnd-kit/abstract/modifiers';
import {RestrictToWindow} from '@dnd-kit/dom/modifiers';

function Draggable({id}) {
const {ref} = useDraggable({
id,
modifiers: [RestrictToVerticalAxis, RestrictToWindow],
});
}
```
22 changes: 22 additions & 0 deletions apps/docs/stories/react/Draggable/Sensors/Sensors.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type {Meta, StoryObj} from '@storybook/react';

import docs from './docs/SensorDocs.mdx';
import {DraggableExample} from '../DraggableExample';

const meta: Meta<typeof DraggableExample> = {
title: 'React/Draggable/Sensors',
component: DraggableExample,
tags: ['autodocs'],
parameters: {
docs: {
page: docs,
},
},
};

export default meta;
type Story = StoryObj<typeof DraggableExample>;

export const Pointer: Story = {
name: 'Default sensors',
};
Loading

0 comments on commit cd485cc

Please sign in to comment.