|
| 1 | +import { setAttrSafe } from '../attributes'; |
| 2 | +import { BtldElementBase } from '../base'; |
| 3 | +import { css, html } from '../literals'; |
| 4 | + |
| 5 | +const detailsCss = css` |
| 6 | + details:has(> details-panel[layout='grid-columns'])::details-content, |
| 7 | + details:has(> details-panel:not([layout]))::details-content { |
| 8 | + content-visibility: visible !important; |
| 9 | + overflow: hidden; |
| 10 | + } |
| 11 | +
|
| 12 | + details:has(> details-panel[layout='flex']) { |
| 13 | + display: contents; |
| 14 | + } |
| 15 | +
|
| 16 | + details:has(> details-panel[layout='flex'])::details-content { |
| 17 | + content-visibility: visible !important; |
| 18 | + display: contents !important; |
| 19 | + } |
| 20 | +`; |
| 21 | + |
| 22 | +const layoutCss = css` |
| 23 | + :where(details-panel[open]) { |
| 24 | + --details-delay: var(--details-delay-opening); |
| 25 | + } |
| 26 | +
|
| 27 | + :where(details-panel:not([open])) { |
| 28 | + --details-delay: var(--details-delay-closing); |
| 29 | + } |
| 30 | +
|
| 31 | + :where(details-panel) { |
| 32 | + --details-ease: ease-in-out; |
| 33 | + --details-duration: 0.15s; |
| 34 | +
|
| 35 | + --details-transition: var(--details-duration) var(--details-ease) var(--details-delay, 0s); |
| 36 | +
|
| 37 | + transition: |
| 38 | + flex-grow var(--details-transition), |
| 39 | + grid-template-rows var(--details-transition), |
| 40 | + grid-template-columns var(--details-transition); |
| 41 | + } |
| 42 | +
|
| 43 | + :where(details-panel:not([layout]), details-panel[layout='grid-columns']) { |
| 44 | + display: grid; |
| 45 | + } |
| 46 | +
|
| 47 | + :where(details-panel:not([layout]):not([open])) { |
| 48 | + grid-template-rows: 0fr; |
| 49 | + --details-transform: var(--details-transform-closed, traslateY(-100%)); |
| 50 | + } |
| 51 | +
|
| 52 | + :where(details-panel:not([layout])[open]) { |
| 53 | + grid-template-rows: 1fr; |
| 54 | + transform: translateY(0); |
| 55 | + } |
| 56 | +
|
| 57 | + :where(details-panel[layout='grid-columns']:not([open])) { |
| 58 | + grid-template-columns: 0fr; |
| 59 | + --details-transform: var(--details-transform-closed, traslateX(-100%)); |
| 60 | + } |
| 61 | +
|
| 62 | + :where(details-panel[layout='grid-columns'][open]) { |
| 63 | + grid-template-columns: 1fr; |
| 64 | + } |
| 65 | +
|
| 66 | + :where(details-panel[layout='flex']:not([open])) { |
| 67 | + flex-grow: 0.00001; |
| 68 | + --details-transform: var(--details-transform-closed); |
| 69 | + } |
| 70 | +
|
| 71 | + :where(details-panel[layout='flex'][open]) { |
| 72 | + flex-grow: 1; |
| 73 | + } |
| 74 | +`; |
| 75 | + |
| 76 | +const easeCss = css` |
| 77 | + details-panel { |
| 78 | + --ease-step-at-start: cubic-bezier(0, 1, 0, 1); |
| 79 | + --ease-step-at-end: cubic-bezier(1, 0, 1, 0); |
| 80 | + --ease-around-closed: var(--ease-step-at-end); |
| 81 | + --ease-around-opened: var(--ease-step-at-start); |
| 82 | + } |
| 83 | +
|
| 84 | + details-panel[open] { |
| 85 | + --ease-around-closed: var(--ease-step-at-start); |
| 86 | + --ease-around-opened: var(--ease-step-at-end); |
| 87 | + } |
| 88 | +`; |
| 89 | + |
| 90 | +const shadowHtml = html` |
| 91 | + <div part="item"> |
| 92 | + <div part="transform"> |
| 93 | + <slot /> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | +`; |
| 97 | + |
| 98 | +const shadowCss = css` |
| 99 | + [part='item'] { |
| 100 | + min-height: 0; |
| 101 | + } |
| 102 | + [part='transform'] { |
| 103 | + transform: var(--details-transform); |
| 104 | + transition: transform var(--details-transition); |
| 105 | + } |
| 106 | +`; |
| 107 | + |
| 108 | +class BtldDetailsPanel extends BtldElementBase { |
| 109 | + static observedAttributes = ['open', 'layout']; |
| 110 | + |
| 111 | + open = this.props.observeBool({ attr: 'open', attrSync: true }, (value) => { |
| 112 | + setAttrSafe(this.details, 'open', value); |
| 113 | + }); |
| 114 | + |
| 115 | + layout = this.props.observeString({ attr: 'layout', attrSync: true }); |
| 116 | + |
| 117 | + details: HTMLDetailsElement | undefined; |
| 118 | + |
| 119 | + constructor() { |
| 120 | + super(); |
| 121 | + this.props.redefine(); |
| 122 | + this.applyShadow(shadowHtml, shadowCss); |
| 123 | + this.apply(detailsCss, layoutCss, easeCss); |
| 124 | + } |
| 125 | + |
| 126 | + override connect() { |
| 127 | + if (this.parentElement instanceof HTMLDetailsElement) { |
| 128 | + this.details = this.parentElement; |
| 129 | + this.props.set('open', this.details.open, { noSetter: true }); |
| 130 | + this.events.add(this.details, 'toggle', ({ newState }) => { |
| 131 | + this.props.set('open', newState === 'open', { noSetter: true }); |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + // TODO make this changeable after connect |
| 136 | + if (this.hasAttribute('toggle-on-click')) { |
| 137 | + this.events.add(this, 'click', () => { |
| 138 | + this.open = !this.open; |
| 139 | + }); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +customElements.define('details-panel', BtldDetailsPanel); |
0 commit comments