Three Blocks

Wave of Connection / source

Open preview
helpers/ExampleCaption.jsPlain text ↗
const CAPTION_STYLES = `
	.tb-example-caption {
		--tb-caption-accent: #d4ff47;
		--tb-caption-bottom: clamp(14px, 3vw, 34px);
		position: absolute;
		right: clamp(14px, 3vw, 38px);
		bottom: var(--tb-caption-bottom);
		z-index: 4;
		box-sizing: border-box;
		width: min(360px, calc(100% - 28px));
		overflow: hidden;
		border: 1px solid rgba(255, 255, 255, .1);
		border-radius: 6px;
		background: rgba(10, 10, 10, .9);
		box-shadow: 0 12px 36px rgba(0, 0, 0, .32);
		color: #ededed;
		font-family: ui-sans-serif, system-ui, sans-serif;
		pointer-events: none;
	}
	.tb-example-caption__toggle {
		display: none;
		list-style: none;
	}
	.tb-example-caption__toggle::-webkit-details-marker { display: none; }
	.tb-example-caption__body {
		display: grid;
		gap: 6px;
		padding: 12px 14px 13px;
		border-left: 2px solid var(--tb-caption-accent);
	}
	.tb-example-caption__eyebrow,
	.tb-example-caption__meta {
		color: #a3a3a3;
		font: 600 9px/1.4 ui-monospace, "SF Mono", monospace;
		letter-spacing: .08em;
		text-transform: uppercase;
	}
	.tb-example-caption__eyebrow {
		display: flex;
		align-items: center;
		gap: 7px;
		letter-spacing: .12em;
	}
	.tb-example-caption__eyebrow::before {
		width: 12px;
		height: 2px;
		background: var(--tb-caption-accent);
		content: "";
	}
	.tb-example-caption__title {
		margin: 0;
		color: #ededed;
		font: 600 15px/1.25 ui-sans-serif, system-ui, sans-serif;
		letter-spacing: -.01em;
	}
	.tb-example-caption__title span { color: #707070; }
	.tb-example-caption__note {
		margin: 0;
		color: #a3a3a3;
		font: 400 11px/1.45 ui-sans-serif, system-ui, sans-serif;
	}
	.tb-example-caption__track,
	.tb-example-caption__links {
		display: flex;
		flex-wrap: wrap;
		gap: 5px 12px;
	}
	.tb-example-caption__track span {
		display: flex;
		align-items: center;
		gap: 5px;
		color: #707070;
		font: 600 9px/1.4 ui-monospace, "SF Mono", monospace;
		letter-spacing: .06em;
		text-transform: uppercase;
	}
	.tb-example-caption__track i { display: block; width: 12px; height: 2px; }
	.tb-example-caption__links {
		margin-top: 3px;
		padding-top: 8px;
		border-top: 1px solid rgba(255, 255, 255, .08);
	}
	.tb-example-caption a {
		color: #ededed;
		font: 500 10px/1.4 ui-sans-serif, system-ui, sans-serif;
		text-decoration-color: rgba(255, 255, 255, .28);
		text-underline-offset: 3px;
		pointer-events: auto;
	}
	.tb-example-caption a:hover { color: var(--tb-caption-accent); }
	.tb-example-caption a:focus-visible,
	.tb-example-caption__toggle:focus-visible { outline: 2px solid var(--tb-caption-accent); outline-offset: -2px; }
	@media (max-width: 620px), (max-height: 520px) {
		.tb-example-caption {
			right: 12px;
			bottom: max(12px, env(safe-area-inset-bottom));
			width: auto;
			max-width: calc(100% - 80px);
			pointer-events: auto;
		}
		.tb-example-caption__toggle {
			display: flex;
			min-height: 36px;
			width: 100%;
			align-items: center;
			justify-content: space-between;
			gap: 12px;
			padding: 0 10px;
			border: 0;
			background: none;
			color: #a3a3a3;
			font: 600 9px/1 ui-monospace, "SF Mono", monospace;
			letter-spacing: .08em;
			text-align: left;
			text-transform: uppercase;
			cursor: pointer;
		}
		.tb-example-caption__toggle::before {
			width: 6px;
			height: 6px;
			flex: 0 0 auto;
			border-radius: 999px;
			background: var(--tb-caption-accent);
			box-shadow: 0 0 10px var(--tb-caption-accent);
			content: "";
		}
		.tb-example-caption__toggle span:first-child { flex: 1; }
		.tb-example-caption__indicator::before { content: "+"; }
		.tb-example-caption__body { display: none; }
		.tb-example-caption[open] {
			left: 12px;
			right: 64px;
			width: auto;
			max-width: none;
			max-height: calc(100% - 104px);
			overflow: auto;
		}
		.tb-example-caption[open] .tb-example-caption__toggle {
			border-bottom: 1px solid rgba(255, 255, 255, .08);
		}
		.tb-example-caption[open] .tb-example-caption__indicator::before { content: "−"; }
		.tb-example-caption[open] .tb-example-caption__body { display: grid; }
	}
`;

export function createExampleCaption( {
	accent = '#d4ff47',
	ariaLabel,
	bottom = 'clamp(14px, 3vw, 34px)',
	content,
	label,
} ) {

	const element = document.createElement( 'details' );
	element.className = 'tb-example-caption';
	element.setAttribute( 'aria-label', ariaLabel );
	element.style.setProperty( '--tb-caption-accent', accent );
	element.style.setProperty( '--tb-caption-bottom', bottom );
	element.innerHTML = `
		<summary class="tb-example-caption__toggle">
			<span>${label}</span><span class="tb-example-caption__indicator" aria-hidden="true"></span>
		</summary>
		<style>${CAPTION_STYLES}</style>
		<div class="tb-example-caption__body">${content}</div>
	`;
	element.open = ! window.matchMedia( '(max-width: 620px), (max-height: 520px)' ).matches;
	return element;

}