-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(aspect-ratio): handle element resize
- Loading branch information
Showing
17 changed files
with
179 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<script setup> | ||
import { vPAspectRatio } from '.' | ||
</script> | ||
|
||
# Aspect Ratio | ||
> CSS Aspect-Ratio alternative using JS | ||
## Usage | ||
|
||
### Simple Usage | ||
|
||
<preview class="items-start space-x-3"> | ||
<div | ||
class="p-4 text-white w-28 bg-primary" | ||
v-p-aspect-ratio="1 / 1"> | ||
1 / 1 | ||
</div> | ||
<div | ||
class="p-4 text-white w-28 bg-primary" | ||
v-p-aspect-ratio="4 / 3"> | ||
4 / 3 | ||
</div> | ||
<div | ||
class="p-4 text-white w-28 bg-primary" | ||
v-p-aspect-ratio="16 / 9"> | ||
16 / 9 | ||
</div> | ||
</preview> | ||
|
||
```vue | ||
<template> | ||
<div | ||
class="p-4 text-white w-28 bg-primary" | ||
v-p-aspect-ratio="1 / 1"> | ||
1 / 1 | ||
</div> | ||
<div | ||
class="p-4 text-white w-28 bg-primary" | ||
v-p-aspect-ratio="4 / 3"> | ||
4 / 3 | ||
</div> | ||
<div | ||
class="p-4 text-white w-28 bg-primary" | ||
v-p-aspect-ratio="16 / 9"> | ||
16 / 9 | ||
</div> | ||
</template> | ||
<script setup> | ||
import { vPAspectRatio } from '@privyid/persona/directive' | ||
</script> | ||
``` | ||
|
||
## Fixed Size | ||
|
||
Add modifier `.fixed` to enable fixed size, it's using style `height` instead of `min-height` | ||
|
||
<preview class="space-x-3"> | ||
<div class="p-4 text-white w-28 bg-primary" v-p-aspect-ratio="1"> | ||
Lorem ipsum dolor sit amet, consectetur adipisicing elit. | ||
</div> | ||
<div class="p-4 text-white w-28 bg-primary" v-p-aspect-ratio.fixed="1"> | ||
Lorem ipsum dolor sit amet, consectetur adipisicing elit. | ||
</div> | ||
</preview> | ||
|
||
```vue | ||
<template> | ||
<div class="p-4 text-white w-28 bg-primary" v-p-aspect-ratio="1"> | ||
Lorem ipsum dolor sit amet, consectetur adipisicing elit. | ||
</div> | ||
<div class="p-4 text-white w-28 bg-primary" v-p-aspect-ratio.fixed="1"> | ||
Lorem ipsum dolor sit amet, consectetur adipisicing elit. | ||
</div> | ||
</template> | ||
``` | ||
|
||
## API | ||
|
||
### Modifiers | ||
|
||
| Modifiers | Description | | ||
|-----------|--------------------| | ||
| `fixed` | Enable fixed sized | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,54 @@ | ||
import { Directive } from 'vue-demi' | ||
|
||
export const pAspectRatio: Directive<HTMLElement, number> = (el, { value, modifiers }) => { | ||
const width = el.clientWidth | ||
const height = width / value | ||
|
||
if (modifiers.fixed) | ||
el.style.height = `${height}px` | ||
else | ||
el.style.minHeight = `${height}px` | ||
let observer: ResizeObserver | ||
|
||
function getObserver () { | ||
if (!observer) { | ||
observer = new ResizeObserver((entries) => { | ||
for (const entry of entries) { | ||
const target = entry.target as HTMLElement | ||
const ratio = Number.parseFloat(target.dataset.aspectRatio) | ||
const fixed = target.dataset.aspectFixed === 'true' | ||
|
||
calculateSize(target, ratio, fixed) | ||
} | ||
}) | ||
} | ||
|
||
return observer | ||
} | ||
|
||
function calculateSize (el: HTMLElement, ratio: number, fixed = false): void { | ||
if (Number.isFinite(ratio)) { | ||
const width = el.clientWidth | ||
const height = width / ratio | ||
|
||
if (fixed) | ||
el.style.height = `${height}px` | ||
else | ||
el.style.minHeight = `${height}px` | ||
} | ||
} | ||
|
||
export const pAspectRatio: Directive<HTMLElement, number> = { | ||
mounted (el, { value, modifiers }) { | ||
el.dataset.aspectRatio = `${value}` | ||
el.dataset.aspectFixed = modifiers.fixed ? 'true' : 'false' | ||
|
||
calculateSize(el, value, modifiers.fixed) | ||
getObserver().observe(el) | ||
}, | ||
|
||
updated (el, { value, modifiers }) { | ||
el.dataset.aspectRatio = `${value}` | ||
el.dataset.aspectFixed = modifiers.fixed ? 'true' : 'false' | ||
|
||
calculateSize(el, value, modifiers.fixed) | ||
}, | ||
|
||
beforeUnmount (el) { | ||
getObserver().unobserve(el) | ||
}, | ||
} | ||
|
||
export const vPAspectRatio = pAspectRatio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters