Skip to content

Commit

Permalink
Add print behavior for components with hidden content
Browse files Browse the repository at this point in the history
  • Loading branch information
hughess committed Jan 3, 2025
1 parent 18ae8a9 commit 27bca8e
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-books-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@evidence-dev/core-components': patch
---

Add print behavior for components with hidden content
48 changes: 37 additions & 11 deletions packages/ui/core-components/src/lib/unsorted/ui/Details.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,50 @@
</script>

<script>
import { slide } from 'svelte/transition';
import { toBoolean } from '../../utils.js';
export let title = 'Details';
export let open = false;
$: open = open === 'true' || open === true;
import { slide } from 'svelte/transition';
$: open = toBoolean(open);
export let printShowAll = true;
$: printShowAll = toBoolean(printShowAll);
let printing = false;
</script>

<div class="mb-4 mt-3">
<button class="text-sm cursor-pointer inline-flex gap-2" on:click={() => (open = !open)}>
<span class={open ? 'marker rotate-marker' : 'marker'} />
<span> {title} </span>
</button>
<svelte:window
on:beforeprint={() => (printing = true)}
on:afterprint={() => (printing = false)}
on:export-beforeprint={() => (printing = true)}
on:export-afterprint={() => (printing = false)}
/>

{#if !printing || !printShowAll}
<div class="mb-4 mt-2">
<button
class="text-sm text-base-content-muted cursor-pointer inline-flex gap-2"
on:click={() => (open = !open)}
>
<span class={open ? 'marker rotate-marker' : 'marker'} />
<span> {title} </span>
</button>

{#if open}
<div class="pl-[calc(0.5rem+10px)] pt-3 mb-6 text-sm" transition:slide|local>
<slot />
</div>
{/if}
</div>
{:else}
<div class="mb-4 mt-2 text-base-content-muted">
<span class="text-sm font-semibold inline-flex"> {title} </span>

{#if open}
<div class="pl-[calc(0.5rem+10px)] pt-3 mb-6" transition:slide|local>
<div class="pt-1 mb-6 text-sm">
<slot />
</div>
{/if}
</div>
</div>
{/if}

<style>
.marker {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@

<script>
import { version } from '$app/environment';
import { toBoolean } from '../../utils.js';
import { fmt } from '@evidence-dev/component-utilities/formatting';
const timestamp = Number(version);
export let prefix = 'Last refreshed';
export let printShowDate = true;
$: printShowDate = toBoolean(printShowDate);
let printing = false;
export let dateFmt = 'h:mmam/pm mmm d, yyyy';
function timeAgo(startTimestamp, endTimestamp) {
const secondsAgo = Math.floor((endTimestamp - startTimestamp) / 1000);
Expand Down Expand Up @@ -37,7 +46,18 @@
const varTimeAgo = timeAgo(timestamp, Date.now());
</script>

<p class="text-sm py-1 cursor-text" title={new Date(timestamp).toLocaleString()}>
<svelte:window
on:beforeprint={() => (printing = true)}
on:afterprint={() => (printing = false)}
on:export-beforeprint={() => (printing = true)}
on:export-afterprint={() => (printing = false)}
/>

<p class="text-sm mb-4 cursor-text" title={new Date(timestamp).toLocaleString()}>
{prefix}
{varTimeAgo}
{#if !printing || !printShowDate}
{varTimeAgo}
{:else}
{fmt(new Date(timestamp), dateFmt)}
{/if}
</p>
22 changes: 20 additions & 2 deletions packages/ui/core-components/src/lib/unsorted/ui/Tabs/Tab.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<script>
import { getContext, onDestroy, onMount } from 'svelte';
import { nanoid } from 'nanoid';
import TabDisplay from './TabDisplay.svelte';
/**
* @type {string}
Expand Down Expand Up @@ -54,8 +55,25 @@
onDestroy(() => {
$context.tabs = $context.tabs.filter((t) => t.internalId !== internalId);
});
const color = $context.color;
</script>
{#if selected}
<slot />
{#if !$context.printing || !$context.printShowAll}
{#if selected}
<div class="mb-5">
<slot />
</div>
{/if}
{:else}
<nav class="my-6 flex flex-wrap gap-x-1 gap-y-1">
{#each $context.tabs as tab}
<TabDisplay id={tab.id} label={tab.label} activeId={id} {color}>
<slot />
</TabDisplay>
{/each}
</nav>
<div class="text-base">
<slot />
</div>
{/if}
43 changes: 30 additions & 13 deletions packages/ui/core-components/src/lib/unsorted/ui/Tabs/Tabs.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
import { onMount, setContext } from 'svelte';
import { writable } from 'svelte/store';
import TabDisplay from './TabDisplay.svelte';
import { toBoolean } from '../../../utils.js';
export let id;
export let color = undefined;
export let printShowAll = true;
$: printShowAll = toBoolean(printShowAll);
let printing = false;
/** @type {import('./index.d.ts').TabsContext} */
const context = writable({ tabs: [], activeId: null });
Expand Down Expand Up @@ -44,27 +48,40 @@
history.replaceState({}, '', url);
}
$: $context.color = color;
$: $context.printing = printing;
$: $context.printShowAll = printShowAll;
setContext('TABS_STORE', context);
const handleTabClick = (id) => {
$context.activeId = id;
};
</script>
<svelte:window
on:beforeprint={() => (printing = true)}
on:afterprint={() => (printing = false)}
on:export-beforeprint={() => (printing = true)}
on:export-afterprint={() => (printing = false)}
/>
<section>
<nav class="my-6 flex flex-wrap gap-x-1 gap-y-1">
{#each $context.tabs as tab}
<TabDisplay
id={tab.id}
label={tab.label}
{color}
activeId={$context.activeId}
on:click={() => handleTabClick(tab.id)}
>
<slot />
</TabDisplay>
{/each}
</nav>
{#if !printing || !printShowAll}
<nav class="my-6 flex flex-wrap gap-x-1 gap-y-1">
{#each $context.tabs as tab}
<TabDisplay
id={tab.id}
label={tab.label}
{color}
activeId={$context.activeId}
on:click={() => handleTabClick(tab.id)}
>
<slot />
</TabDisplay>
{/each}
</nav>
{/if}
<div class="text-base">
<slot />
</div>
Expand Down
4 changes: 2 additions & 2 deletions sites/docs/pages/components/ui/details/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The details component allows you to add a collapsible section to your markdown.
<DocTab>
<div slot='preview'>
<Details title="Definitions">

Definition of metrics in Solutions Targets

### Time to Proposal
Expand Down Expand Up @@ -50,7 +50,7 @@ The details component allows you to add a collapsible section to your markdown.
<DocTab>
<div slot='preview'>
<Details title="Definitions" open=true>

Definition of metrics in Solutions Targets

### Time to Proposal
Expand Down

0 comments on commit 27bca8e

Please sign in to comment.