@corvu-next/transition-size 0.1.0
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 +21 -0
- package/README.md +54 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +94 -0
- package/dist/index.jsx +94 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-2025 Jasmin Noetzli
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<a href="https://corvu.dev/docs/utilities/transition-size">
|
|
3
|
+
<img src="https://corvu.dev/readme/solid-transition-size.png" width=1000 alt="Solid Transition Size" />
|
|
4
|
+
</a>
|
|
5
|
+
</div>
|
|
6
|
+
<br />
|
|
7
|
+
|
|
8
|
+
SolidJS utility which makes it possible to transition the width and height of elements that don't have a fixed size applied.
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- Works with any CSS transition configuration
|
|
13
|
+
- Specify which dimension to observe (width, height, or both)
|
|
14
|
+
- Uses a ResizeObserver to detect changes in the size of the element
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
The utility returns two signals: `transitioning` and `transitionSize`. `transitioning` can be used to know when the transition is happening, and `transitionSize` returns the fixed size of the element while transitioning. You **have** to use it to style the element you want to transition.
|
|
19
|
+
|
|
20
|
+
```tsx
|
|
21
|
+
import createTransitionSize from 'solid-transition-size';
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
For a very simple example we can take the `<details>` element and transition the height when it is toggled:
|
|
25
|
+
```tsx
|
|
26
|
+
const Details = () => {
|
|
27
|
+
const [ref, setRef] = createSignal<HTMLElement | null>(null)
|
|
28
|
+
const { transitionSize } = createTransitionSize({
|
|
29
|
+
element: ref,
|
|
30
|
+
dimension: 'height',
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
const height = () => {
|
|
34
|
+
if (!transitionSize()) return undefined
|
|
35
|
+
return transitionSize() + 'px'
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<details
|
|
40
|
+
ref={setRef}
|
|
41
|
+
class="transition-[height]"
|
|
42
|
+
style={{
|
|
43
|
+
height: height(),
|
|
44
|
+
}}
|
|
45
|
+
>
|
|
46
|
+
<summary>Show detail</summary>
|
|
47
|
+
Detail
|
|
48
|
+
</details>
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Further Reading
|
|
54
|
+
This utility is from the maintainers of [corvu](https://corvu.dev), a collection of unstyled, accessible and customizable UI primitives for SolidJS. It is also documented in the corvu docs under [Transition Size](https://corvu.dev/docs/utilities/transition-size).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { MaybeAccessor } from '@corvu-next/utils/reactivity';
|
|
2
|
+
export { MaybeAccessor } from '@corvu-next/utils/reactivity';
|
|
3
|
+
import { Accessor } from 'solid-js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Utility that uses a `ResizeObserver` to provide the size of an element before and after resize. Used to transition the width/height of elements that don't have a fixed size applied.
|
|
7
|
+
*
|
|
8
|
+
* @param props.element - The element to transition.
|
|
9
|
+
* @param props.enabled - Whether the utility is enabled. *Default = `true`*
|
|
10
|
+
* @param props.dimension - The dimension to transition. *Default = `'both'`*
|
|
11
|
+
* @returns ```typescript
|
|
12
|
+
* {
|
|
13
|
+
* transitioning: () => boolean
|
|
14
|
+
* transitionSize: () => number | [number, number] | null
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
declare function createTransitionSize(props: {
|
|
19
|
+
element: MaybeAccessor<HTMLElement | null>;
|
|
20
|
+
enabled?: MaybeAccessor<boolean>;
|
|
21
|
+
dimension?: MaybeAccessor<'both'>;
|
|
22
|
+
}): {
|
|
23
|
+
transitioning: Accessor<boolean>;
|
|
24
|
+
transitionSize: Accessor<[number, number] | null>;
|
|
25
|
+
};
|
|
26
|
+
declare function createTransitionSize(props: {
|
|
27
|
+
element: MaybeAccessor<HTMLElement | null>;
|
|
28
|
+
enabled?: MaybeAccessor<boolean>;
|
|
29
|
+
dimension: MaybeAccessor<'width' | 'height'>;
|
|
30
|
+
}): {
|
|
31
|
+
transitioning: Accessor<boolean>;
|
|
32
|
+
transitionSize: Accessor<number | null>;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export { createTransitionSize as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { access } from '@corvu-next/utils/reactivity';
|
|
2
|
+
import { createSignal, createEffect } from 'solid-js';
|
|
3
|
+
import { afterPaint } from '@corvu-next/utils/dom';
|
|
4
|
+
|
|
5
|
+
// src/transitionSize.ts
|
|
6
|
+
function createTransitionSize(props) {
|
|
7
|
+
const [transitioning, setTransitioning] = createSignal(false);
|
|
8
|
+
const [transitionSize, setTransitionSize] = createSignal(null);
|
|
9
|
+
let startSize = null;
|
|
10
|
+
createEffect(() => {
|
|
11
|
+
const element = access(props.element);
|
|
12
|
+
const enabled = access(props.enabled ?? true);
|
|
13
|
+
if (!element || !enabled) return;
|
|
14
|
+
const observer = new ResizeObserver(resizeObserverCallback);
|
|
15
|
+
observer.observe(element);
|
|
16
|
+
element.addEventListener("transitionend", reset);
|
|
17
|
+
return () => {
|
|
18
|
+
observer.disconnect();
|
|
19
|
+
element.removeEventListener("transitionend", reset);
|
|
20
|
+
reset();
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
const resizeObserverCallback = ([entry]) => {
|
|
24
|
+
if (transitioning()) return;
|
|
25
|
+
const target = entry.target;
|
|
26
|
+
const currentSize = [
|
|
27
|
+
target.offsetWidth,
|
|
28
|
+
target.offsetHeight
|
|
29
|
+
];
|
|
30
|
+
const dimension = access(props.dimension ?? "both");
|
|
31
|
+
if (dimension === "both") {
|
|
32
|
+
if (!startSize) {
|
|
33
|
+
startSize = currentSize;
|
|
34
|
+
} else if (startSize[0] !== currentSize[0] && startSize[1] !== currentSize[1]) {
|
|
35
|
+
setTransitionSize(startSize);
|
|
36
|
+
setTransitioning(true);
|
|
37
|
+
afterPaint(() => {
|
|
38
|
+
setTransitionSize(currentSize);
|
|
39
|
+
const transitionDuration = parseFloat(
|
|
40
|
+
getComputedStyle(entry.target).transitionDuration
|
|
41
|
+
);
|
|
42
|
+
if (transitionDuration === 0) {
|
|
43
|
+
reset();
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
} else {
|
|
48
|
+
if (!startSize) {
|
|
49
|
+
startSize = currentSize;
|
|
50
|
+
} else if (getSizeOfDimension(dimension, startSize) !== getSizeOfDimension(dimension, currentSize)) {
|
|
51
|
+
setTransitionSize(getSizeOfDimension(dimension, startSize));
|
|
52
|
+
setTransitioning(true);
|
|
53
|
+
afterPaint(() => {
|
|
54
|
+
setTransitionSize(getSizeOfDimension(dimension, currentSize));
|
|
55
|
+
const transitionDuration = parseFloat(
|
|
56
|
+
getComputedStyle(entry.target).transitionDuration
|
|
57
|
+
);
|
|
58
|
+
if (transitionDuration === 0) {
|
|
59
|
+
reset();
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
const reset = () => {
|
|
66
|
+
if (!transitioning()) return;
|
|
67
|
+
const element = access(props.element);
|
|
68
|
+
if (element) {
|
|
69
|
+
startSize = [element.offsetWidth, element.offsetHeight];
|
|
70
|
+
} else {
|
|
71
|
+
startSize = null;
|
|
72
|
+
}
|
|
73
|
+
setTransitioning(false);
|
|
74
|
+
setTransitionSize(null);
|
|
75
|
+
};
|
|
76
|
+
return {
|
|
77
|
+
transitioning,
|
|
78
|
+
transitionSize
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
var getSizeOfDimension = (dimension, size) => {
|
|
82
|
+
switch (dimension) {
|
|
83
|
+
case "width":
|
|
84
|
+
return size[0];
|
|
85
|
+
case "height":
|
|
86
|
+
return size[1];
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
var transitionSize_default = createTransitionSize;
|
|
90
|
+
|
|
91
|
+
// src/index.ts
|
|
92
|
+
var index_default = transitionSize_default;
|
|
93
|
+
|
|
94
|
+
export { index_default as default };
|
package/dist/index.jsx
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// src/transitionSize.ts
|
|
2
|
+
import { access } from "@corvu-next/utils/reactivity";
|
|
3
|
+
import { createEffect, createSignal } from "solid-js";
|
|
4
|
+
import { afterPaint } from "@corvu-next/utils/dom";
|
|
5
|
+
function createTransitionSize(props) {
|
|
6
|
+
const [transitioning, setTransitioning] = createSignal(false);
|
|
7
|
+
const [transitionSize, setTransitionSize] = createSignal(null);
|
|
8
|
+
let startSize = null;
|
|
9
|
+
createEffect(() => {
|
|
10
|
+
const element = access(props.element);
|
|
11
|
+
const enabled = access(props.enabled ?? true);
|
|
12
|
+
if (!element || !enabled) return;
|
|
13
|
+
const observer = new ResizeObserver(resizeObserverCallback);
|
|
14
|
+
observer.observe(element);
|
|
15
|
+
element.addEventListener("transitionend", reset);
|
|
16
|
+
return () => {
|
|
17
|
+
observer.disconnect();
|
|
18
|
+
element.removeEventListener("transitionend", reset);
|
|
19
|
+
reset();
|
|
20
|
+
};
|
|
21
|
+
});
|
|
22
|
+
const resizeObserverCallback = ([entry]) => {
|
|
23
|
+
if (transitioning()) return;
|
|
24
|
+
const target = entry.target;
|
|
25
|
+
const currentSize = [
|
|
26
|
+
target.offsetWidth,
|
|
27
|
+
target.offsetHeight
|
|
28
|
+
];
|
|
29
|
+
const dimension = access(props.dimension ?? "both");
|
|
30
|
+
if (dimension === "both") {
|
|
31
|
+
if (!startSize) {
|
|
32
|
+
startSize = currentSize;
|
|
33
|
+
} else if (startSize[0] !== currentSize[0] && startSize[1] !== currentSize[1]) {
|
|
34
|
+
setTransitionSize(startSize);
|
|
35
|
+
setTransitioning(true);
|
|
36
|
+
afterPaint(() => {
|
|
37
|
+
setTransitionSize(currentSize);
|
|
38
|
+
const transitionDuration = parseFloat(
|
|
39
|
+
getComputedStyle(entry.target).transitionDuration
|
|
40
|
+
);
|
|
41
|
+
if (transitionDuration === 0) {
|
|
42
|
+
reset();
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
} else {
|
|
47
|
+
if (!startSize) {
|
|
48
|
+
startSize = currentSize;
|
|
49
|
+
} else if (getSizeOfDimension(dimension, startSize) !== getSizeOfDimension(dimension, currentSize)) {
|
|
50
|
+
setTransitionSize(getSizeOfDimension(dimension, startSize));
|
|
51
|
+
setTransitioning(true);
|
|
52
|
+
afterPaint(() => {
|
|
53
|
+
setTransitionSize(getSizeOfDimension(dimension, currentSize));
|
|
54
|
+
const transitionDuration = parseFloat(
|
|
55
|
+
getComputedStyle(entry.target).transitionDuration
|
|
56
|
+
);
|
|
57
|
+
if (transitionDuration === 0) {
|
|
58
|
+
reset();
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
const reset = () => {
|
|
65
|
+
if (!transitioning()) return;
|
|
66
|
+
const element = access(props.element);
|
|
67
|
+
if (element) {
|
|
68
|
+
startSize = [element.offsetWidth, element.offsetHeight];
|
|
69
|
+
} else {
|
|
70
|
+
startSize = null;
|
|
71
|
+
}
|
|
72
|
+
setTransitioning(false);
|
|
73
|
+
setTransitionSize(null);
|
|
74
|
+
};
|
|
75
|
+
return {
|
|
76
|
+
transitioning,
|
|
77
|
+
transitionSize
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
var getSizeOfDimension = (dimension, size) => {
|
|
81
|
+
switch (dimension) {
|
|
82
|
+
case "width":
|
|
83
|
+
return size[0];
|
|
84
|
+
case "height":
|
|
85
|
+
return size[1];
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
var transitionSize_default = createTransitionSize;
|
|
89
|
+
|
|
90
|
+
// src/index.ts
|
|
91
|
+
var index_default = transitionSize_default;
|
|
92
|
+
export {
|
|
93
|
+
index_default as default
|
|
94
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@corvu-next/transition-size",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "SolidJS 2 utility for animating element size transitions.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Jasmin Noetzli (upstream), @corvu-next contributors (fork)"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"sideEffects": false,
|
|
11
|
+
"main": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"solid": "./dist/index.jsx",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"default": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"typesVersions": {
|
|
24
|
+
"*": {
|
|
25
|
+
"*": [
|
|
26
|
+
"./dist/index.d.ts"
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@corvu-next/utils": "~0.1.0"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"solid-js": "2.0.0-beta.15"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@solidjs/web": "2.0.0-beta.15",
|
|
38
|
+
"esbuild-plugin-solid": "^0.6.0",
|
|
39
|
+
"solid-js": "2.0.0-beta.15",
|
|
40
|
+
"tsup": "^8.5.0",
|
|
41
|
+
"typescript": "^5.9.2"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "tsup",
|
|
45
|
+
"clean": "rm -rf .turbo dist node_modules",
|
|
46
|
+
"lint": "tsc --noEmit"
|
|
47
|
+
}
|
|
48
|
+
}
|