@bento/pressable 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +9 -0
- package/README.mdx +95 -0
- package/dist/index.cjs +46 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +44 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.js +39 -0
- package/dist/index.js.map +1 -0
- package/dist/pressable.module-JXZDAJ3U.module.css +4 -0
- package/package.json +69 -0
- package/src/index.tsx +86 -0
- package/src/pressable.module.css +4 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 GoDaddy Operating Company, LLC.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.mdx
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { Meta, ArgTypes, Story, Controls, Source } from '@storybook/addon-docs/blocks';
|
|
2
|
+
import * as Stories from './pressable.stories.tsx';
|
|
3
|
+
import DivExample from './examples/pressable-div.tsx?raw';
|
|
4
|
+
import LinkExample from './examples/pressable-link-example.tsx?raw';
|
|
5
|
+
import CustomExample from './examples/pressable-custom.tsx?raw';
|
|
6
|
+
|
|
7
|
+
<Meta of={Stories} name="Overview" />
|
|
8
|
+
|
|
9
|
+
# Pressable
|
|
10
|
+
|
|
11
|
+
The `@bento/pressable` package provides a standardized foundation for interactive elements in the Bento library. It exports the **Pressable** primitive, which provides consistent press interactions and accessibility features for building interactive components.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install --save @bento/pressable
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Props
|
|
20
|
+
|
|
21
|
+
The `@bento/pressable` package exports the `Pressable` primitive:
|
|
22
|
+
|
|
23
|
+
```jsx
|
|
24
|
+
import { Pressable } from '@bento/pressable';
|
|
25
|
+
|
|
26
|
+
<Pressable onPress={() => console.log('Div pressed!')}>
|
|
27
|
+
<div>Press me</div>
|
|
28
|
+
</Pressable>
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The following properties are available to be used on the `Pressable` primitive:
|
|
32
|
+
|
|
33
|
+
<ArgTypes of={Stories.Props} />
|
|
34
|
+
|
|
35
|
+
For all other properties specified on the `Pressable` primitive, the component will pass them down to the direct child element of the component. Which would be the equivalent of you adding them directly to the child element.
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
import { Pressable } from '@bento/pressable';
|
|
39
|
+
|
|
40
|
+
function MyComponent() {
|
|
41
|
+
return (
|
|
42
|
+
<Pressable onPress={() => console.log('Pressed!')}>
|
|
43
|
+
<div>Press me</div>
|
|
44
|
+
</Pressable>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Examples
|
|
50
|
+
|
|
51
|
+
#### Basic Div Element
|
|
52
|
+
|
|
53
|
+
The simplest way to use Pressable is to wrap a div element. The component will handle all the necessary accessibility features and interaction states.
|
|
54
|
+
|
|
55
|
+
<Source language='tsx' code={ DivExample } />
|
|
56
|
+
|
|
57
|
+
#### Link Element
|
|
58
|
+
|
|
59
|
+
You can also make link elements pressable while maintaining their semantic meaning and default browser behavior.
|
|
60
|
+
|
|
61
|
+
<Source language='tsx' code={ LinkExample } />
|
|
62
|
+
|
|
63
|
+
#### Custom Component
|
|
64
|
+
|
|
65
|
+
For custom components, you need to forward the ref and pass props to the underlying element to ensure proper functionality.
|
|
66
|
+
|
|
67
|
+
<Source language='tsx' code={ CustomExample } />
|
|
68
|
+
|
|
69
|
+
## Accessibility
|
|
70
|
+
|
|
71
|
+
The Pressable component automatically handles accessibility features:
|
|
72
|
+
|
|
73
|
+
- **Keyboard Navigation**: Supports Enter and Space key activation
|
|
74
|
+
- **Focus Management**: Proper focus handling and focus-visible states
|
|
75
|
+
- **ARIA Attributes**: Automatically applies appropriate ARIA attributes
|
|
76
|
+
- **Screen Reader Support**: Works seamlessly with assistive technologies
|
|
77
|
+
|
|
78
|
+
### Data Attributes
|
|
79
|
+
|
|
80
|
+
The following data attributes are exposed and can be used for styling:
|
|
81
|
+
|
|
82
|
+
- `data-pressed` - True when the button is being pressed
|
|
83
|
+
- `data-hovered` - True when the button is hovered
|
|
84
|
+
- `data-focused` - True when the button has focus
|
|
85
|
+
- `data-focus-visible` - True when focus should be visible (keyboard navigation)
|
|
86
|
+
|
|
87
|
+
```css
|
|
88
|
+
[data-pressed='true'] {
|
|
89
|
+
background-color: #ccc;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
[data-focus-visible='true'] {
|
|
93
|
+
outline: 2px solid blue;
|
|
94
|
+
}
|
|
95
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var reactAria = require('react-aria');
|
|
4
|
+
var utils = require('@react-aria/utils');
|
|
5
|
+
var useDataAttributes = require('@bento/use-data-attributes');
|
|
6
|
+
var useProps = require('@bento/use-props');
|
|
7
|
+
var slots = require('@bento/slots');
|
|
8
|
+
var React = require('react');
|
|
9
|
+
var style = require('./pressable.module-JXZDAJ3U.module.css');
|
|
10
|
+
|
|
11
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
12
|
+
|
|
13
|
+
var React__default = /*#__PURE__*/_interopDefault(React);
|
|
14
|
+
var style__default = /*#__PURE__*/_interopDefault(style);
|
|
15
|
+
|
|
16
|
+
// src/index.tsx
|
|
17
|
+
var Pressable = slots.withSlots("BentoPressable", function Pressable2(args) {
|
|
18
|
+
const { props, apply } = useProps.useProps(args);
|
|
19
|
+
const ref = React.useRef(null);
|
|
20
|
+
const { children, ...restProps } = props;
|
|
21
|
+
const child = React__default.default.Children.only(children);
|
|
22
|
+
const { focusableProps } = reactAria.useFocusable(restProps, ref);
|
|
23
|
+
const { focusProps, isFocused, isFocusVisible } = reactAria.useFocusRing(restProps);
|
|
24
|
+
const { hoverProps, isHovered } = reactAria.useHover(restProps);
|
|
25
|
+
const { pressProps, isPressed } = reactAria.usePress({ ...restProps, ref });
|
|
26
|
+
return React__default.default.cloneElement(child, {
|
|
27
|
+
...apply(
|
|
28
|
+
{
|
|
29
|
+
...reactAria.mergeProps(pressProps, focusProps, focusableProps, hoverProps),
|
|
30
|
+
className: style__default.default.pressable
|
|
31
|
+
},
|
|
32
|
+
["onPress", "onPressStart", "onPressEnd", "onPressUp", "onPressChange", "children"]
|
|
33
|
+
),
|
|
34
|
+
...useDataAttributes.useDataAttributes({
|
|
35
|
+
pressed: isPressed,
|
|
36
|
+
hovered: isHovered,
|
|
37
|
+
focused: isFocused,
|
|
38
|
+
focusVisible: isFocusVisible
|
|
39
|
+
}),
|
|
40
|
+
ref: utils.mergeRefs(child.ref, ref)
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
exports.Pressable = Pressable;
|
|
45
|
+
//# sourceMappingURL=index.cjs.map
|
|
46
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.tsx"],"names":["withSlots","Pressable","useProps","useRef","React","useFocusable","useFocusRing","useHover","usePress","mergeProps","style","useDataAttributes","mergeRefs"],"mappings":";;;;;;;;;;;;;;;;AA2DO,IAAM,SAAA,GAAYA,eAAA,CAAU,gBAAA,EAAkB,SAASC,WAAU,IAAA,EAAsB;AAC5F,EAAA,MAAM,EAAE,KAAA,EAAO,KAAA,EAAM,GAAIC,kBAAS,IAAI,CAAA;AACtC,EAAA,MAAM,GAAA,GAAMC,aAA2B,IAAI,CAAA;AAC3C,EAAA,MAAM,EAAE,QAAA,EAAU,GAAG,SAAA,EAAU,GAAI,KAAA;AACnC,EAAA,MAAM,KAAA,GAAQC,sBAAA,CAAM,QAAA,CAAS,IAAA,CAAK,QAAQ,CAAA;AAC1C,EAAA,MAAM,EAAE,cAAA,EAAe,GAAIC,sBAAA,CAAa,WAAW,GAAG,CAAA;AACtD,EAAA,MAAM,EAAE,UAAA,EAAY,SAAA,EAAW,cAAA,EAAe,GAAIC,uBAAa,SAAS,CAAA;AACxE,EAAA,MAAM,EAAE,UAAA,EAAY,SAAA,EAAU,GAAIC,mBAAS,SAAS,CAAA;AACpD,EAAA,MAAM,EAAE,YAAY,SAAA,EAAU,GAAIC,mBAAS,EAAE,GAAG,SAAA,EAAW,GAAA,EAAK,CAAA;AAEhE,EAAA,OAAOJ,sBAAA,CAAM,aAAa,KAAA,EAAO;AAAA,IAC/B,GAAG,KAAA;AAAA,MACD;AAAA,QACE,GAAGK,oBAAA,CAAW,UAAA,EAAY,UAAA,EAAY,gBAAgB,UAAU,CAAA;AAAA,QAChE,WAAWC,sBAAA,CAAM;AAAA,OACnB;AAAA,MACA,CAAC,SAAA,EAAW,cAAA,EAAgB,YAAA,EAAc,WAAA,EAAa,iBAAiB,UAAU;AAAA,KACpF;AAAA,IACA,GAAGC,mCAAA,CAAkB;AAAA,MACnB,OAAA,EAAS,SAAA;AAAA,MACT,OAAA,EAAS,SAAA;AAAA,MACT,OAAA,EAAS,SAAA;AAAA,MACT,YAAA,EAAc;AAAA,KACf,CAAA;AAAA,IACD,GAAA,EAAKC,eAAA,CAAW,KAAA,CAA2C,GAAA,EAAK,GAAG;AAAA,GACpE,CAAA;AACH,CAAC","file":"index.cjs","sourcesContent":["import {\n usePress,\n useFocusRing,\n useHover,\n mergeProps,\n type PressProps,\n useFocusable,\n type PressEvent\n} from 'react-aria';\nimport { mergeRefs } from '@react-aria/utils';\nimport { useDataAttributes } from '@bento/use-data-attributes';\nimport { useProps } from '@bento/use-props';\nimport { withSlots } from '@bento/slots';\nimport React, { type HTMLAttributes, useRef } from 'react';\nimport style from './pressable.module.css';\n\nexport interface PressableProps extends PressProps, Omit<HTMLAttributes<HTMLElement>, keyof PressProps> {\n /** A single React element that will be made pressable. */\n children: React.ReactElement;\n\n /**\n * Handler that is called when the pressable is pressed.\n * Similar to the standard `onClick` event, but normalized to handle all interaction methods consistently.\n */\n onPress?: (e: PressEvent) => void;\n\n /** Handler that is called when a press interaction starts. */\n onPressStart?: (e: PressEvent) => void;\n\n /**\n * Handler that is called when a press interaction ends, either\n * over the target or when the pointer leaves the target.\n */\n onPressEnd?: (e: PressEvent) => void;\n\n /** Handler that is called when the press state changes. */\n onPressChange?: (isPressed: boolean) => void;\n\n /**\n * Handler that is called when a press is released over the target, regardless of\n * whether it started on the target or not.\n */\n onPressUp?: (e: PressEvent) => void;\n}\n\n/**\n *\n * A behavioral primitive that can be used to make any element pressable while maintaining\n * consistent behavior across devices (mouse, touch, keyboard). It handles edge cases like\n * touch movement and quick releases, and provides proper ARIA attributes for accessibility.\n *\n * @example\n * ```tsx\n * // Make a div pressable\n * <Pressable onPress={handlePress}>\n * <div>Click here</div>\n * </Pressable>\n * ```\n */\nexport const Pressable = withSlots('BentoPressable', function Pressable(args: PressableProps) {\n const { props, apply } = useProps(args);\n const ref = useRef<HTMLElement | null>(null);\n const { children, ...restProps } = props;\n const child = React.Children.only(children);\n const { focusableProps } = useFocusable(restProps, ref);\n const { focusProps, isFocused, isFocusVisible } = useFocusRing(restProps);\n const { hoverProps, isHovered } = useHover(restProps);\n const { pressProps, isPressed } = usePress({ ...restProps, ref });\n\n return React.cloneElement(child, {\n ...apply(\n {\n ...mergeProps(pressProps, focusProps, focusableProps, hoverProps),\n className: style.pressable\n },\n ['onPress', 'onPressStart', 'onPressEnd', 'onPressUp', 'onPressChange', 'children']\n ),\n ...useDataAttributes({\n pressed: isPressed,\n hovered: isHovered,\n focused: isFocused,\n focusVisible: isFocusVisible\n }),\n ref: mergeRefs((child as { ref?: React.Ref<HTMLElement> }).ref, ref)\n });\n});\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import * as _bento_slots from '@bento/slots';
|
|
2
|
+
import { PressProps, PressEvent } from 'react-aria';
|
|
3
|
+
import React, { HTMLAttributes } from 'react';
|
|
4
|
+
|
|
5
|
+
interface PressableProps extends PressProps, Omit<HTMLAttributes<HTMLElement>, keyof PressProps> {
|
|
6
|
+
/** A single React element that will be made pressable. */
|
|
7
|
+
children: React.ReactElement;
|
|
8
|
+
/**
|
|
9
|
+
* Handler that is called when the pressable is pressed.
|
|
10
|
+
* Similar to the standard `onClick` event, but normalized to handle all interaction methods consistently.
|
|
11
|
+
*/
|
|
12
|
+
onPress?: (e: PressEvent) => void;
|
|
13
|
+
/** Handler that is called when a press interaction starts. */
|
|
14
|
+
onPressStart?: (e: PressEvent) => void;
|
|
15
|
+
/**
|
|
16
|
+
* Handler that is called when a press interaction ends, either
|
|
17
|
+
* over the target or when the pointer leaves the target.
|
|
18
|
+
*/
|
|
19
|
+
onPressEnd?: (e: PressEvent) => void;
|
|
20
|
+
/** Handler that is called when the press state changes. */
|
|
21
|
+
onPressChange?: (isPressed: boolean) => void;
|
|
22
|
+
/**
|
|
23
|
+
* Handler that is called when a press is released over the target, regardless of
|
|
24
|
+
* whether it started on the target or not.
|
|
25
|
+
*/
|
|
26
|
+
onPressUp?: (e: PressEvent) => void;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
*
|
|
30
|
+
* A behavioral primitive that can be used to make any element pressable while maintaining
|
|
31
|
+
* consistent behavior across devices (mouse, touch, keyboard). It handles edge cases like
|
|
32
|
+
* touch movement and quick releases, and provides proper ARIA attributes for accessibility.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```tsx
|
|
36
|
+
* // Make a div pressable
|
|
37
|
+
* <Pressable onPress={handlePress}>
|
|
38
|
+
* <div>Click here</div>
|
|
39
|
+
* </Pressable>
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
declare const Pressable: React.NamedExoticComponent<PressableProps & _bento_slots.Slots>;
|
|
43
|
+
|
|
44
|
+
export { Pressable, type PressableProps };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import * as _bento_slots from '@bento/slots';
|
|
2
|
+
import { PressProps, PressEvent } from 'react-aria';
|
|
3
|
+
import React, { HTMLAttributes } from 'react';
|
|
4
|
+
|
|
5
|
+
interface PressableProps extends PressProps, Omit<HTMLAttributes<HTMLElement>, keyof PressProps> {
|
|
6
|
+
/** A single React element that will be made pressable. */
|
|
7
|
+
children: React.ReactElement;
|
|
8
|
+
/**
|
|
9
|
+
* Handler that is called when the pressable is pressed.
|
|
10
|
+
* Similar to the standard `onClick` event, but normalized to handle all interaction methods consistently.
|
|
11
|
+
*/
|
|
12
|
+
onPress?: (e: PressEvent) => void;
|
|
13
|
+
/** Handler that is called when a press interaction starts. */
|
|
14
|
+
onPressStart?: (e: PressEvent) => void;
|
|
15
|
+
/**
|
|
16
|
+
* Handler that is called when a press interaction ends, either
|
|
17
|
+
* over the target or when the pointer leaves the target.
|
|
18
|
+
*/
|
|
19
|
+
onPressEnd?: (e: PressEvent) => void;
|
|
20
|
+
/** Handler that is called when the press state changes. */
|
|
21
|
+
onPressChange?: (isPressed: boolean) => void;
|
|
22
|
+
/**
|
|
23
|
+
* Handler that is called when a press is released over the target, regardless of
|
|
24
|
+
* whether it started on the target or not.
|
|
25
|
+
*/
|
|
26
|
+
onPressUp?: (e: PressEvent) => void;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
*
|
|
30
|
+
* A behavioral primitive that can be used to make any element pressable while maintaining
|
|
31
|
+
* consistent behavior across devices (mouse, touch, keyboard). It handles edge cases like
|
|
32
|
+
* touch movement and quick releases, and provides proper ARIA attributes for accessibility.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```tsx
|
|
36
|
+
* // Make a div pressable
|
|
37
|
+
* <Pressable onPress={handlePress}>
|
|
38
|
+
* <div>Click here</div>
|
|
39
|
+
* </Pressable>
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
declare const Pressable: React.NamedExoticComponent<PressableProps & _bento_slots.Slots>;
|
|
43
|
+
|
|
44
|
+
export { Pressable, type PressableProps };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { useFocusable, useFocusRing, useHover, usePress, mergeProps } from 'react-aria';
|
|
2
|
+
import { mergeRefs } from '@react-aria/utils';
|
|
3
|
+
import { useDataAttributes } from '@bento/use-data-attributes';
|
|
4
|
+
import { useProps } from '@bento/use-props';
|
|
5
|
+
import { withSlots } from '@bento/slots';
|
|
6
|
+
import React, { useRef } from 'react';
|
|
7
|
+
import style from './pressable.module-JXZDAJ3U.module.css';
|
|
8
|
+
|
|
9
|
+
// src/index.tsx
|
|
10
|
+
var Pressable = withSlots("BentoPressable", function Pressable2(args) {
|
|
11
|
+
const { props, apply } = useProps(args);
|
|
12
|
+
const ref = useRef(null);
|
|
13
|
+
const { children, ...restProps } = props;
|
|
14
|
+
const child = React.Children.only(children);
|
|
15
|
+
const { focusableProps } = useFocusable(restProps, ref);
|
|
16
|
+
const { focusProps, isFocused, isFocusVisible } = useFocusRing(restProps);
|
|
17
|
+
const { hoverProps, isHovered } = useHover(restProps);
|
|
18
|
+
const { pressProps, isPressed } = usePress({ ...restProps, ref });
|
|
19
|
+
return React.cloneElement(child, {
|
|
20
|
+
...apply(
|
|
21
|
+
{
|
|
22
|
+
...mergeProps(pressProps, focusProps, focusableProps, hoverProps),
|
|
23
|
+
className: style.pressable
|
|
24
|
+
},
|
|
25
|
+
["onPress", "onPressStart", "onPressEnd", "onPressUp", "onPressChange", "children"]
|
|
26
|
+
),
|
|
27
|
+
...useDataAttributes({
|
|
28
|
+
pressed: isPressed,
|
|
29
|
+
hovered: isHovered,
|
|
30
|
+
focused: isFocused,
|
|
31
|
+
focusVisible: isFocusVisible
|
|
32
|
+
}),
|
|
33
|
+
ref: mergeRefs(child.ref, ref)
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
export { Pressable };
|
|
38
|
+
//# sourceMappingURL=index.js.map
|
|
39
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.tsx"],"names":["Pressable"],"mappings":";;;;;;;;;AA2DO,IAAM,SAAA,GAAY,SAAA,CAAU,gBAAA,EAAkB,SAASA,WAAU,IAAA,EAAsB;AAC5F,EAAA,MAAM,EAAE,KAAA,EAAO,KAAA,EAAM,GAAI,SAAS,IAAI,CAAA;AACtC,EAAA,MAAM,GAAA,GAAM,OAA2B,IAAI,CAAA;AAC3C,EAAA,MAAM,EAAE,QAAA,EAAU,GAAG,SAAA,EAAU,GAAI,KAAA;AACnC,EAAA,MAAM,KAAA,GAAQ,KAAA,CAAM,QAAA,CAAS,IAAA,CAAK,QAAQ,CAAA;AAC1C,EAAA,MAAM,EAAE,cAAA,EAAe,GAAI,YAAA,CAAa,WAAW,GAAG,CAAA;AACtD,EAAA,MAAM,EAAE,UAAA,EAAY,SAAA,EAAW,cAAA,EAAe,GAAI,aAAa,SAAS,CAAA;AACxE,EAAA,MAAM,EAAE,UAAA,EAAY,SAAA,EAAU,GAAI,SAAS,SAAS,CAAA;AACpD,EAAA,MAAM,EAAE,YAAY,SAAA,EAAU,GAAI,SAAS,EAAE,GAAG,SAAA,EAAW,GAAA,EAAK,CAAA;AAEhE,EAAA,OAAO,KAAA,CAAM,aAAa,KAAA,EAAO;AAAA,IAC/B,GAAG,KAAA;AAAA,MACD;AAAA,QACE,GAAG,UAAA,CAAW,UAAA,EAAY,UAAA,EAAY,gBAAgB,UAAU,CAAA;AAAA,QAChE,WAAW,KAAA,CAAM;AAAA,OACnB;AAAA,MACA,CAAC,SAAA,EAAW,cAAA,EAAgB,YAAA,EAAc,WAAA,EAAa,iBAAiB,UAAU;AAAA,KACpF;AAAA,IACA,GAAG,iBAAA,CAAkB;AAAA,MACnB,OAAA,EAAS,SAAA;AAAA,MACT,OAAA,EAAS,SAAA;AAAA,MACT,OAAA,EAAS,SAAA;AAAA,MACT,YAAA,EAAc;AAAA,KACf,CAAA;AAAA,IACD,GAAA,EAAK,SAAA,CAAW,KAAA,CAA2C,GAAA,EAAK,GAAG;AAAA,GACpE,CAAA;AACH,CAAC","file":"index.js","sourcesContent":["import {\n usePress,\n useFocusRing,\n useHover,\n mergeProps,\n type PressProps,\n useFocusable,\n type PressEvent\n} from 'react-aria';\nimport { mergeRefs } from '@react-aria/utils';\nimport { useDataAttributes } from '@bento/use-data-attributes';\nimport { useProps } from '@bento/use-props';\nimport { withSlots } from '@bento/slots';\nimport React, { type HTMLAttributes, useRef } from 'react';\nimport style from './pressable.module.css';\n\nexport interface PressableProps extends PressProps, Omit<HTMLAttributes<HTMLElement>, keyof PressProps> {\n /** A single React element that will be made pressable. */\n children: React.ReactElement;\n\n /**\n * Handler that is called when the pressable is pressed.\n * Similar to the standard `onClick` event, but normalized to handle all interaction methods consistently.\n */\n onPress?: (e: PressEvent) => void;\n\n /** Handler that is called when a press interaction starts. */\n onPressStart?: (e: PressEvent) => void;\n\n /**\n * Handler that is called when a press interaction ends, either\n * over the target or when the pointer leaves the target.\n */\n onPressEnd?: (e: PressEvent) => void;\n\n /** Handler that is called when the press state changes. */\n onPressChange?: (isPressed: boolean) => void;\n\n /**\n * Handler that is called when a press is released over the target, regardless of\n * whether it started on the target or not.\n */\n onPressUp?: (e: PressEvent) => void;\n}\n\n/**\n *\n * A behavioral primitive that can be used to make any element pressable while maintaining\n * consistent behavior across devices (mouse, touch, keyboard). It handles edge cases like\n * touch movement and quick releases, and provides proper ARIA attributes for accessibility.\n *\n * @example\n * ```tsx\n * // Make a div pressable\n * <Pressable onPress={handlePress}>\n * <div>Click here</div>\n * </Pressable>\n * ```\n */\nexport const Pressable = withSlots('BentoPressable', function Pressable(args: PressableProps) {\n const { props, apply } = useProps(args);\n const ref = useRef<HTMLElement | null>(null);\n const { children, ...restProps } = props;\n const child = React.Children.only(children);\n const { focusableProps } = useFocusable(restProps, ref);\n const { focusProps, isFocused, isFocusVisible } = useFocusRing(restProps);\n const { hoverProps, isHovered } = useHover(restProps);\n const { pressProps, isPressed } = usePress({ ...restProps, ref });\n\n return React.cloneElement(child, {\n ...apply(\n {\n ...mergeProps(pressProps, focusProps, focusableProps, hoverProps),\n className: style.pressable\n },\n ['onPress', 'onPressStart', 'onPressEnd', 'onPressUp', 'onPressChange', 'children']\n ),\n ...useDataAttributes({\n pressed: isPressed,\n hovered: isHovered,\n focused: isFocused,\n focusVisible: isFocusVisible\n }),\n ref: mergeRefs((child as { ref?: React.Ref<HTMLElement> }).ref, ref)\n });\n});\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bento/pressable",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Pressable primitive provides consistent press interactions and accessibility features for building interactive components",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsup-node",
|
|
10
|
+
"lint": "biome lint && tsc",
|
|
11
|
+
"posttest": "npm run lint",
|
|
12
|
+
"pretest": "npm run build",
|
|
13
|
+
"test": "vitest --run",
|
|
14
|
+
"test:watch": "vitest"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/godaddy/bento.git"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"accessibility",
|
|
22
|
+
"aria",
|
|
23
|
+
"bento",
|
|
24
|
+
"component",
|
|
25
|
+
"interactive",
|
|
26
|
+
"library",
|
|
27
|
+
"pressable",
|
|
28
|
+
"react"
|
|
29
|
+
],
|
|
30
|
+
"author": "GoDaddy Operating Company, LLC",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/godaddy/bento/issues"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/godaddy/bento#readme",
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"src",
|
|
39
|
+
"package.json"
|
|
40
|
+
],
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@bento/slots": "^0.1.1",
|
|
43
|
+
"@bento/use-data-attributes": "^0.1.0",
|
|
44
|
+
"@bento/use-props": "^0.1.0",
|
|
45
|
+
"@react-aria/utils": "^3.30.0",
|
|
46
|
+
"react-aria": "^3.44.0"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"react": "18.x || 19.x",
|
|
50
|
+
"react-dom": "18.x || 19.x"
|
|
51
|
+
},
|
|
52
|
+
"exports": {
|
|
53
|
+
".": {
|
|
54
|
+
"import": {
|
|
55
|
+
"types": "./dist/index.d.ts",
|
|
56
|
+
"default": "./dist/index.js"
|
|
57
|
+
},
|
|
58
|
+
"require": {
|
|
59
|
+
"types": "./dist/index.d.cts",
|
|
60
|
+
"default": "./dist/index.cjs"
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"./package.json": "./package.json"
|
|
64
|
+
},
|
|
65
|
+
"publishConfig": {
|
|
66
|
+
"access": "public",
|
|
67
|
+
"registry": "https://registry.npmjs.org/"
|
|
68
|
+
}
|
|
69
|
+
}
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import {
|
|
2
|
+
usePress,
|
|
3
|
+
useFocusRing,
|
|
4
|
+
useHover,
|
|
5
|
+
mergeProps,
|
|
6
|
+
type PressProps,
|
|
7
|
+
useFocusable,
|
|
8
|
+
type PressEvent
|
|
9
|
+
} from 'react-aria';
|
|
10
|
+
import { mergeRefs } from '@react-aria/utils';
|
|
11
|
+
import { useDataAttributes } from '@bento/use-data-attributes';
|
|
12
|
+
import { useProps } from '@bento/use-props';
|
|
13
|
+
import { withSlots } from '@bento/slots';
|
|
14
|
+
import React, { type HTMLAttributes, useRef } from 'react';
|
|
15
|
+
import style from './pressable.module.css';
|
|
16
|
+
|
|
17
|
+
export interface PressableProps extends PressProps, Omit<HTMLAttributes<HTMLElement>, keyof PressProps> {
|
|
18
|
+
/** A single React element that will be made pressable. */
|
|
19
|
+
children: React.ReactElement;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Handler that is called when the pressable is pressed.
|
|
23
|
+
* Similar to the standard `onClick` event, but normalized to handle all interaction methods consistently.
|
|
24
|
+
*/
|
|
25
|
+
onPress?: (e: PressEvent) => void;
|
|
26
|
+
|
|
27
|
+
/** Handler that is called when a press interaction starts. */
|
|
28
|
+
onPressStart?: (e: PressEvent) => void;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Handler that is called when a press interaction ends, either
|
|
32
|
+
* over the target or when the pointer leaves the target.
|
|
33
|
+
*/
|
|
34
|
+
onPressEnd?: (e: PressEvent) => void;
|
|
35
|
+
|
|
36
|
+
/** Handler that is called when the press state changes. */
|
|
37
|
+
onPressChange?: (isPressed: boolean) => void;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Handler that is called when a press is released over the target, regardless of
|
|
41
|
+
* whether it started on the target or not.
|
|
42
|
+
*/
|
|
43
|
+
onPressUp?: (e: PressEvent) => void;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
*
|
|
48
|
+
* A behavioral primitive that can be used to make any element pressable while maintaining
|
|
49
|
+
* consistent behavior across devices (mouse, touch, keyboard). It handles edge cases like
|
|
50
|
+
* touch movement and quick releases, and provides proper ARIA attributes for accessibility.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```tsx
|
|
54
|
+
* // Make a div pressable
|
|
55
|
+
* <Pressable onPress={handlePress}>
|
|
56
|
+
* <div>Click here</div>
|
|
57
|
+
* </Pressable>
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export const Pressable = withSlots('BentoPressable', function Pressable(args: PressableProps) {
|
|
61
|
+
const { props, apply } = useProps(args);
|
|
62
|
+
const ref = useRef<HTMLElement | null>(null);
|
|
63
|
+
const { children, ...restProps } = props;
|
|
64
|
+
const child = React.Children.only(children);
|
|
65
|
+
const { focusableProps } = useFocusable(restProps, ref);
|
|
66
|
+
const { focusProps, isFocused, isFocusVisible } = useFocusRing(restProps);
|
|
67
|
+
const { hoverProps, isHovered } = useHover(restProps);
|
|
68
|
+
const { pressProps, isPressed } = usePress({ ...restProps, ref });
|
|
69
|
+
|
|
70
|
+
return React.cloneElement(child, {
|
|
71
|
+
...apply(
|
|
72
|
+
{
|
|
73
|
+
...mergeProps(pressProps, focusProps, focusableProps, hoverProps),
|
|
74
|
+
className: style.pressable
|
|
75
|
+
},
|
|
76
|
+
['onPress', 'onPressStart', 'onPressEnd', 'onPressUp', 'onPressChange', 'children']
|
|
77
|
+
),
|
|
78
|
+
...useDataAttributes({
|
|
79
|
+
pressed: isPressed,
|
|
80
|
+
hovered: isHovered,
|
|
81
|
+
focused: isFocused,
|
|
82
|
+
focusVisible: isFocusVisible
|
|
83
|
+
}),
|
|
84
|
+
ref: mergeRefs((child as { ref?: React.Ref<HTMLElement> }).ref, ref)
|
|
85
|
+
});
|
|
86
|
+
});
|