|
| 1 | +{/* Copyright 2023 Adobe. All rights reserved. |
| 2 | +This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. You may obtain a copy |
| 4 | +of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | +Unless required by applicable law or agreed to in writing, software distributed under |
| 6 | +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 7 | +OF ANY KIND, either express or implied. See the License for the specific language |
| 8 | +governing permissions and limitations under the License. */} |
| 9 | + |
| 10 | +import {ExampleLayout} from '@react-spectrum/docs'; |
| 11 | +export default ExampleLayout; |
| 12 | + |
| 13 | +import docs from 'docs:react-aria-components'; |
| 14 | +import {TypeLink} from '@react-spectrum/docs'; |
| 15 | +import styles from '@react-spectrum/docs/src/docs.css'; |
| 16 | +import Button from '@react-spectrum/docs/pages/assets/component-illustrations/Button.svg'; |
| 17 | +import {ExampleCard} from '@react-spectrum/docs/src/ExampleCard'; |
| 18 | + |
| 19 | +--- |
| 20 | +keywords: [example, button, aria, accessibility, react, component] |
| 21 | +type: component |
| 22 | +image: ripple-button.png |
| 23 | +description: A ripple animated button styled with Tailwind CSS. |
| 24 | +--- |
| 25 | + |
| 26 | +# Ripple Button |
| 27 | + |
| 28 | +An animated Ripple [Button](../Button.html) styled with [Tailwind CSS](https://tailwindcss.com/). |
| 29 | + |
| 30 | +## Example |
| 31 | + |
| 32 | +```tsx import |
| 33 | +import './tailwind.global.css'; |
| 34 | +``` |
| 35 | + |
| 36 | +```tsx example standalone |
| 37 | +import {Button} from 'react-aria-components'; |
| 38 | +import {useEffect, useRef, useState} from 'react'; |
| 39 | +import Airplane from '@spectrum-icons/workflow/Airplane'; |
| 40 | + |
| 41 | +function RippleButton(props) { |
| 42 | + const [coords, setCoords] = useState({x: -1, y: -1}); |
| 43 | + const [isRippling, setIsRippling] = useState(false); |
| 44 | + |
| 45 | + let timeout = useRef<ReturnType<typeof setTimeout> | null>(null); |
| 46 | + let onPress = (e) => { |
| 47 | + setCoords({x: e.x, y: e.y}); |
| 48 | + if (e.x !== -1 && e.y !== -1) { |
| 49 | + setIsRippling(true); |
| 50 | + timeout.current = setTimeout(() => setIsRippling(false), 600); |
| 51 | + } |
| 52 | + }; |
| 53 | + useEffect(() => { |
| 54 | + return () => { |
| 55 | + clearTimeout(timeout.current); |
| 56 | + }; |
| 57 | + }, []); |
| 58 | + |
| 59 | + return ( |
| 60 | + <div className="bg-gradient-to-r from-teal-300 to-cyan-500 p-12 rounded-lg flex justify-center"> |
| 61 | + <Button |
| 62 | + onPress={onPress} |
| 63 | + className={` |
| 64 | + relative overflow-hidden min-h-16 |
| 65 | + inline-flex items-center justify-center rounded-md bg-black bg-opacity-50 bg-clip-padding border border-white/20 px-3.5 py-2 font-medium text-white |
| 66 | + hover:bg-opacity-60 pressed:bg-opacity-70 transition-colors cursor-default outline-none focus-visible:ring-2 focus-visible:ring-white/75`}> |
| 67 | + {isRippling ? ( |
| 68 | + <div |
| 69 | + className="ripple" |
| 70 | + style={{ |
| 71 | + left: coords.x - 15, |
| 72 | + top: coords.y - 15 |
| 73 | + }} /> |
| 74 | + ) : ( |
| 75 | + '' |
| 76 | + )} |
| 77 | + <span className="flex items-center gap-2">{props.children}</span> |
| 78 | + </Button> |
| 79 | + </div> |
| 80 | + ) |
| 81 | +} |
| 82 | + |
| 83 | +<RippleButton><Airplane size="S" /> Book flight</RippleButton> |
| 84 | +``` |
| 85 | +```css |
| 86 | +div.ripple { |
| 87 | + position: absolute; |
| 88 | + height: 30px; |
| 89 | + width: 30px; |
| 90 | + border-radius: 50%; |
| 91 | + transform: scale(0); |
| 92 | + opacity: 1; |
| 93 | + animation: ripple 600ms linear; |
| 94 | + background-color: rgba(255, 255, 255, .6); |
| 95 | +} |
| 96 | +@keyframes ripple { |
| 97 | + from { |
| 98 | + transform: scale(0); |
| 99 | + opacity: 1; |
| 100 | + } |
| 101 | + to { |
| 102 | + transform: scale(6); |
| 103 | + opacity: 0; |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### Tailwind config |
| 109 | + |
| 110 | +This example uses the [tailwindcss-react-aria-components](../styling.html#plugin) plugin. Add it to your `tailwind.config.js`: |
| 111 | + |
| 112 | +```tsx |
| 113 | +module.exports = { |
| 114 | + // ... |
| 115 | + plugins: [ |
| 116 | + require('tailwindcss-react-aria-components') |
| 117 | + ] |
| 118 | +}; |
| 119 | +``` |
| 120 | + |
| 121 | +## Components |
| 122 | + |
| 123 | +<section className={styles.cardGroup} data-size="small"> |
| 124 | + |
| 125 | + <ExampleCard |
| 126 | + url="../Button.html" |
| 127 | + title="Button" |
| 128 | + description="A button allows a user to perform an action."> |
| 129 | + <Button /> |
| 130 | + </ExampleCard> |
| 131 | + |
| 132 | +</section> |
| 133 | + |
0 commit comments