Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/@react-spectrum/s2/src/pressScale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@
import {composeRenderProps} from 'react-aria-components/composeRenderProps';
import {CSSProperties, RefObject} from 'react';

/**
* Returns a render prop style function that applies a subtle Spectrum "press" scale
* effect to an element while it is being pressed.
*
* @param ref - A ref to the element receiving the press effect.
* @param style - An optional base style object, or a render prop function returning
* one.
* @returns A render prop function suitable for passing to a React Aria component's
* `style` prop.
*
* @example
* ```tsx
* import {Button} from 'react-aria-components/Button';
* import {pressScale} from '@react-spectrum/s2';
* import {useRef} from 'react';
*
* function MyButton(props) {
* let ref = useRef(null);
* return <Button {...props} ref={ref} style={pressScale(ref, props.style)} />;
* }
* ```
*/
export function pressScale<R extends {isPressed: boolean}>(ref: RefObject<HTMLElement | null>, style?: CSSProperties | ((renderProps: R) => CSSProperties)): (renderProps: R) => CSSProperties {
return composeRenderProps(style, (style, renderProps: R) => {
if (renderProps.isPressed && ref.current) {
Expand Down
4 changes: 4 additions & 0 deletions packages/dev/s2-docs/pages/s2/style-macro.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ See the [Icons](icons#api) page for more information.

<FunctionJSDoc function={docs.exports.mergeStyles} />

### pressScale

<FunctionJSDoc function={docs.exports.pressScale} />

### centerPadding

<FunctionJSDoc function={styleDocs.exports.centerPadding} />
Expand Down
Loading