-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
666 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
'number-flow': patch | ||
'@number-flow/svelte': patch | ||
'@number-flow/react': patch | ||
'@number-flow/vue': patch | ||
--- | ||
|
||
Expose parts for styling support |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,53 @@ | ||
import type { Data } from './formatter' | ||
import { charHeight, maskHeight, SlottedTag } from './styles' | ||
import { BROWSER } from './util/env' | ||
import type { Data, KeyedNumberPart } from './formatter' | ||
import { css, html } from './util/string' | ||
import { charHeight, halfMaskHeight, maskHeight } from './styles' | ||
import { BROWSER } from 'esm-env' | ||
|
||
export const ServerSafeHTMLElement = BROWSER | ||
? HTMLElement | ||
: (class {} as unknown as typeof HTMLElement) // for types | ||
|
||
export type RenderProps = Pick<Data, 'valueAsString'> & { willChange?: boolean } | ||
const styles = css` | ||
:host { | ||
display: inline-block; | ||
direction: ltr; | ||
white-space: nowrap; | ||
line-height: ${charHeight} !important; | ||
} | ||
// Could eventually use DSD e.g. | ||
// `<template shadowroot="open" shadowrootmode="open"> | ||
export const render = ({ valueAsString, willChange }: RenderProps) => | ||
`<${SlottedTag} style="font-kerning: none; display: inline-block; line-height: ${charHeight}; padding: ${maskHeight} 0;${willChange ? 'will-change: transform' : ''}">${valueAsString}</${SlottedTag}>` | ||
span { | ||
display: inline-block; | ||
} | ||
:host([data-will-change]) span { | ||
will-change: transform; | ||
} | ||
.number, | ||
.digit { | ||
padding: ${halfMaskHeight} 0; | ||
} | ||
.symbol { | ||
white-space: pre; /* some symbols are spaces or thin spaces */ | ||
} | ||
` | ||
|
||
const renderPart = (part: KeyedNumberPart) => | ||
`<span class="${part.type === 'integer' || part.type === 'fraction' ? 'digit' : 'symbol'}" part="${part.type === 'integer' || part.type === 'fraction' ? `digit ${part.type}-digit` : part.type}">${part.value}</span>` | ||
|
||
const renderSection = (section: KeyedNumberPart[], part: string) => | ||
`<span part="${part}">${section.reduce((str, p) => str + renderPart(p), '')}</span>` | ||
|
||
export const renderInnerHTML = (data: Data) => | ||
// shadowroot="open" non-standard attribute for old Chrome: | ||
html`<template shadowroot="open" shadowrootmode="open" | ||
><style> | ||
${styles}</style | ||
>${renderSection(data.pre, 'left')}<span part="number" class="number" | ||
>${renderSection(data.integer, 'integer')}${renderSection(data.fraction, 'fraction')}</span | ||
>${renderSection(data.post, 'right')}</template | ||
><span | ||
style="font-kerning: none; display: inline-block; line-height: ${charHeight} !important; padding: ${maskHeight} 0;" | ||
>${data.valueAsString}</span | ||
>` // ^ fallback for browsers that don't support DSD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const html = String.raw | ||
export const css = String.raw |
Oops, something went wrong.