Skip to content

Commit

Permalink
add prop text_color to ElementTile
Browse files Browse the repository at this point in the history
add test for props props show_symbol, show_number, show_name
  • Loading branch information
janosh committed Mar 19, 2023
1 parent c1d0f2a commit 79b1eb4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/lib/ColorBar.svelte
Expand Up @@ -9,7 +9,7 @@
export let text_style: string | null = null
export let wrapper_style: string | null = null
export let tick_labels: (string | number)[] | number = 0
export let range: [number, number] = []
export let range: [number, number] = [0, 1]
export let tick_side: 'top' | 'bottom' | 'center' = `bottom`
// TODO vertical not fully implemented yet
export let orientation: 'horizontal' | 'vertical' = `horizontal`
Expand Down
6 changes: 4 additions & 2 deletions src/lib/ElementTile.svelte
Expand Up @@ -17,6 +17,7 @@
export let href: string | null = null
// at what background color lightness text color switches from black to white
export let text_color_threshold = 0.7
export let text_color: string | null = null
type $$Events = PeriodicTableEvents // for type-safe event listening on this component
Expand Down Expand Up @@ -51,8 +52,9 @@
}
let tile: HTMLElement
$: text_color =
luminance(get_bg_color(tile, bg_color)) > text_color_threshold ? `black` : `white`
$: if (text_color_threshold != null)
text_color =
luminance(get_bg_color(tile, bg_color)) > text_color_threshold ? `black` : `white`
</script>

<svelte:element
Expand Down
4 changes: 3 additions & 1 deletion src/routes/(demos)/element-tile/+page.svx
@@ -1,4 +1,6 @@
`ElementTile.svelte` automatically changes text color to ensure high contrast with its background. If its background is transparent, it traverses up the DOM tree to find the first element with non-transparent background color.
`ElementTile.svelte` automatically changes text color to ensure high contrast with its background. If its background is transparent, it traverses up the DOM tree to find the first element with non-transparent background color. This an, of course, go wrong e.g. if the tile is absolutely positioned outside its parent element. In that case, pass an explicit `text_color` prop and `text_color_threshold={null}` to `ElementTile` to override the automatic color selection.

```svelte example stackblitz code_above

```svelte example stackblitz code_above
<script>
Expand Down
49 changes: 23 additions & 26 deletions tests/unit/element-tile.test.ts
Expand Up @@ -53,7 +53,7 @@ describe(`ElementTile`, () => {
props: { element: rand_element, bg_color: `red` },
})

const node = doc_query(`.element-tile`) as HTMLElement
const node = doc_query(`.element-tile`)

expect(node.style.backgroundColor).toBe(`red`)
})
Expand Down Expand Up @@ -121,29 +121,26 @@ describe(`ElementTile`, () => {
}
)
})
// test(`show_symbol, show_name and show_number props control labels`, async () => {
// // test all 2^3 true/false combinations of show_symbol, show_name, show_number
// for (let idx = 0; idx < 1 << 3; idx++) {
// // const show_name = Boolean(idx & 1)
// const show_name = true
// // const show_number = Boolean(idx & (1 << 1))
// const show_number = true
// const show_symbol = Boolean(idx & (1 << 2))
// // const show_symbol = true

// let expected = ``
// if (show_number) expected += rand_element.number
// if (show_symbol) expected += ` ${rand_element.symbol}`
// if (show_name) expected += ` ${rand_element.name}`
// new ElementTile({
// target: document.body,
// props: { element: rand_element, show_symbol, show_name, show_number },
// })

// const div = document.querySelector(`.element-tile`)
// console.log(`node.innerHTML`, div.innerHTML)

// expect(div.textContent?.trim()).toBe(expected.trim())
// }
// })
test.each([
[true, true, true],
[false, false, false],
[true, false, true],
[false, false, true],
])(
`props show_symbol = %s, show_number = %s, show_name = %s`,
async (show_name, show_number, show_symbol) => {
let expected = ``
if (show_number) expected += rand_element.number
if (show_symbol) expected += ` ${rand_element.symbol}`
if (show_name) expected += ` ${rand_element.name}`
new ElementTile({
target: document.body,
props: { element: rand_element, show_symbol, show_name, show_number },
})

const tile = doc_query(`.element-tile`)

expect(tile.textContent?.trim()).toBe(expected.trim())
}
)
})

0 comments on commit 79b1eb4

Please sign in to comment.