mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 10:21:05 +00:00
fix(expandable-tile): set tile height using resize observer (#1738)
* fix(expandable-tile): set tile max height using Resize Observer * docs(expandable-tile): update and re-work examples
This commit is contained in:
parent
4460f5b5f8
commit
a369962fdf
2 changed files with 45 additions and 13 deletions
|
@ -3,7 +3,29 @@
|
||||||
import Preview from "../../components/Preview.svelte";
|
import Preview from "../../components/Preview.svelte";
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
## Default (unexpanded)
|
## Default
|
||||||
|
|
||||||
|
This component uses a [resize observer](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver) to determine the height of the above-the-fold content.
|
||||||
|
|
||||||
|
It's unexpanded by default.
|
||||||
|
|
||||||
|
<ExpandableTile>
|
||||||
|
<div slot="above">
|
||||||
|
<div>
|
||||||
|
Above the fold content here
|
||||||
|
</div>
|
||||||
|
<br />
|
||||||
|
<br />
|
||||||
|
<div>
|
||||||
|
More above the fold content
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div slot="below">Below the fold content here</div>
|
||||||
|
</ExpandableTile>
|
||||||
|
|
||||||
|
## Custom tile heights
|
||||||
|
|
||||||
|
Set a custom height for the tiles on the "above" and "below" slots.
|
||||||
|
|
||||||
<ExpandableTile>
|
<ExpandableTile>
|
||||||
<div slot="above" style="height: 10rem">Above the fold content here</div>
|
<div slot="above" style="height: 10rem">Above the fold content here</div>
|
||||||
|
@ -13,22 +35,22 @@
|
||||||
## Expanded
|
## Expanded
|
||||||
|
|
||||||
<ExpandableTile expanded>
|
<ExpandableTile expanded>
|
||||||
<div slot="above" style="height: 10rem">Above the fold content here</div>
|
<div slot="above">Above the fold content here</div>
|
||||||
<div slot="below" style="height: 10rem">Below the fold content here</div>
|
<div slot="below">Below the fold content here</div>
|
||||||
</ExpandableTile>
|
</ExpandableTile>
|
||||||
|
|
||||||
## Light variant
|
## Light variant
|
||||||
|
|
||||||
<ExpandableTile light>
|
<ExpandableTile light>
|
||||||
<div slot="above" style="height: 10rem">Above the fold content here</div>
|
<div slot="above">Above the fold content here</div>
|
||||||
<div slot="below" style="height: 10rem">Below the fold content here</div>
|
<div slot="below">Below the fold content here</div>
|
||||||
</ExpandableTile>
|
</ExpandableTile>
|
||||||
|
|
||||||
## With icon labels
|
## With icon labels
|
||||||
|
|
||||||
<ExpandableTile tileExpandedLabel="View less" tileCollapsedLabel="View more">
|
<ExpandableTile tileExpandedLabel="View less" tileCollapsedLabel="View more">
|
||||||
<div slot="above" style="height: 10rem">Above the fold content here</div>
|
<div slot="above">Above the fold content here</div>
|
||||||
<div slot="below" style="height: 10rem">Below the fold content here</div>
|
<div slot="below">Below the fold content here</div>
|
||||||
</ExpandableTile>
|
</ExpandableTile>
|
||||||
|
|
||||||
## With interactive content
|
## With interactive content
|
||||||
|
@ -36,7 +58,7 @@
|
||||||
For tiles containing interactive content, use `stopPropagation` to prevent the tile from toggling.
|
For tiles containing interactive content, use `stopPropagation` to prevent the tile from toggling.
|
||||||
|
|
||||||
<ExpandableTile tileExpandedLabel="View less" tileCollapsedLabel="View more">
|
<ExpandableTile tileExpandedLabel="View less" tileCollapsedLabel="View more">
|
||||||
<div slot="above" style="height: 10rem">
|
<div slot="above">
|
||||||
<a href="/" on:click|preventDefault|stopPropagation={() => console.log("Hello world")}>
|
<a href="/" on:click|preventDefault|stopPropagation={() => console.log("Hello world")}>
|
||||||
Native element
|
Native element
|
||||||
</a>
|
</a>
|
||||||
|
@ -48,5 +70,5 @@ For tiles containing interactive content, use `stopPropagation` to prevent the t
|
||||||
Svelte component
|
Svelte component
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
<div slot="below" style="height: 10rem">Below the fold content here</div>
|
<div slot="below">Below the fold content here</div>
|
||||||
</ExpandableTile>
|
</ExpandableTile>
|
||||||
|
|
|
@ -32,11 +32,23 @@
|
||||||
/** Obtain a reference to the top-level element */
|
/** Obtain a reference to the top-level element */
|
||||||
export let ref = null;
|
export let ref = null;
|
||||||
|
|
||||||
import { afterUpdate } from "svelte";
|
import { afterUpdate, onMount } from "svelte";
|
||||||
import ChevronDown from "../icons/ChevronDown.svelte";
|
import ChevronDown from "../icons/ChevronDown.svelte";
|
||||||
|
|
||||||
let refAbove = null;
|
let refAbove = null;
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
const resizeObserver = new ResizeObserver(([elem]) => {
|
||||||
|
tileMaxHeight = elem.contentRect.height;
|
||||||
|
});
|
||||||
|
|
||||||
|
resizeObserver.observe(refAbove);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
resizeObserver.disconnect();
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
afterUpdate(() => {
|
afterUpdate(() => {
|
||||||
if (tileMaxHeight === 0) {
|
if (tileMaxHeight === 0) {
|
||||||
tileMaxHeight = refAbove.getBoundingClientRect().height;
|
tileMaxHeight = refAbove.getBoundingClientRect().height;
|
||||||
|
@ -62,10 +74,8 @@
|
||||||
class:bx--tile--expandable="{true}"
|
class:bx--tile--expandable="{true}"
|
||||||
class:bx--tile--is-expanded="{expanded}"
|
class:bx--tile--is-expanded="{expanded}"
|
||||||
class:bx--tile--light="{light}"
|
class:bx--tile--light="{light}"
|
||||||
|
style:max-height="{expanded ? "none" : `${tileMaxHeight + tilePadding}px`}"
|
||||||
{...$$restProps}
|
{...$$restProps}
|
||||||
style="{expanded
|
|
||||||
? $$restProps.style
|
|
||||||
: `${$$restProps.style}; max-height: ${tileMaxHeight + tilePadding}px`}"
|
|
||||||
on:click
|
on:click
|
||||||
on:click="{() => {
|
on:click="{() => {
|
||||||
expanded = !expanded;
|
expanded = !expanded;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue