Skip to content
This repository was archived by the owner on Jul 15, 2024. It is now read-only.

Commit a7cf57c

Browse files
committed
Update README.md
1 parent 70ccc11 commit a7cf57c

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

README.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
![License](https://img.shields.io/github/license/aerni/tailwindcss-rfs?style=flat-square)
44

55
# Alpine Resize
6-
This is a convenience wrapper for Resize Observer that allows you to easily react when an element is resized.
7-
8-
One possible use case is if you need to dynamically set the `sizes` attribute on the `<source>` tag of a `<picture>` element.
6+
This is a convenience wrapper around ResizeObserver that allows you to easily run code whenever an element is resized. One possible use case is to dynamically set the `sizes` attribute on the `<source>` tag of a `<picture>` element.
97

108
## Installation
119
You can use this plugin by either including it from a `<script>` tag or installing it via NPM:
@@ -37,16 +35,35 @@ import resize from '@aerni/alpine-resize'
3735
Alpine.plugin(resize)
3836
```
3937

40-
4138
## Usage
42-
Add `x-resize` to any element within an Alpine component. When that component is resized, the provided expression will execute.
39+
Add `x-resize` to any element within an Alpine component. The provided expression will execute as soon as the element is resized.
4340

44-
For example, in the following snippet, `resized` will remain `false` until the element is resized. At that point, the expression will execute and `resized` will become `true`:
41+
In this example, the element will show as soon as it is resized.
4542

4643
```html
4744
<div x-data="{ resized: false }" x-resize="resized = true">
4845
<div x-show="resized">
49-
I was just resized!
46+
This element shows as soon as it is resized
5047
</div>
5148
</div>
5249
```
50+
51+
You can also provide a callback to access the `ResizeObserverEntry` to do fancy things like changing an elements font size based on its width.
52+
53+
```html
54+
<div
55+
x-data="{
56+
width: 0,
57+
minFontSize: 15,
58+
maxFontSize: 50,
59+
}"
60+
x-resize="resizeObserverEntry => {
61+
width = resizeObserverEntry.contentRect.width
62+
}"
63+
:style="{
64+
'font-size': `${Math.min(maxFontSize, Math.max(minFontSize, width * 0.05))}px`,
65+
}"
66+
>
67+
The font-size of this element will change based on its width
68+
</div>
69+
```

0 commit comments

Comments
 (0)