| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- import * as React from 'react'
- import type { TLTheme } from '+types'
-
- const styles = new Map<string, HTMLStyleElement>()
-
- type AnyTheme = Record<string, string>
-
- function makeCssTheme<T = AnyTheme>(prefix: string, theme: T) {
- return Object.keys(theme).reduce((acc, key) => {
- const value = theme[key as keyof T]
- if (value) {
- return acc + `${`--${prefix}-${key}`}: ${value};\n`
- }
- return acc
- }, '')
- }
-
- function useTheme<T = AnyTheme>(prefix: string, theme: T, selector = ':root') {
- React.useLayoutEffect(() => {
- const style = document.createElement('style')
- const cssTheme = makeCssTheme(prefix, theme)
-
- style.setAttribute('id', `${prefix}-theme`)
- style.setAttribute('data-selector', selector)
- style.innerHTML = `
- ${selector} {
- ${cssTheme}
- }
- `
-
- document.head.appendChild(style)
-
- return () => {
- if (style && document.head.contains(style)) {
- document.head.removeChild(style)
- }
- }
- }, [prefix, theme, selector])
- }
-
- function useStyle(uid: string, rules: string) {
- React.useLayoutEffect(() => {
- if (styles.get(uid)) {
- return () => void null
- }
-
- const style = document.createElement('style')
- style.innerHTML = rules
- style.setAttribute('id', uid)
- document.head.appendChild(style)
- styles.set(uid, style)
-
- return () => {
- if (style && document.head.contains(style)) {
- document.head.removeChild(style)
- styles.delete(uid)
- }
- }
- }, [uid, rules])
- }
-
- const css = (strings: TemplateStringsArray, ...args: unknown[]) =>
- strings.reduce(
- (acc, string, index) => acc + string + (index < args.length ? args[index] : ''),
- ''
- )
-
- const defaultTheme: TLTheme = {
- brushFill: 'rgba(0,0,0,.05)',
- brushStroke: 'rgba(0,0,0,.25)',
- selectStroke: 'rgb(66, 133, 244)',
- selectFill: 'rgba(65, 132, 244, 0.05)',
- background: 'rgb(248, 249, 250)',
- foreground: 'rgb(51, 51, 51)',
- }
-
- const tlcss = css`
- @font-face {
- font-family: 'Recursive';
- font-style: normal;
- font-weight: 500;
- font-display: swap;
- src: url(https://fonts.gstatic.com/s/recursive/v23/8vI-7wMr0mhh-RQChyHEH06TlXhq_gukbYrFMk1QuAIcyEwG_X-dpEfaE5YaERmK-CImKsvxvU-MXGX2fSqasNfUlTGZnI14ZeY.woff2)
- format('woff2');
- unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC,
- U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
- }
-
- @font-face {
- font-family: 'Recursive';
- font-style: normal;
- font-weight: 700;
- font-display: swap;
- src: url(https://fonts.gstatic.com/s/recursive/v23/8vI-7wMr0mhh-RQChyHEH06TlXhq_gukbYrFMk1QuAIcyEwG_X-dpEfaE5YaERmK-CImKsvxvU-MXGX2fSqasNfUlTGZnI14ZeY.woff2)
- format('woff2');
- unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC,
- U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
- }
-
- @font-face {
- font-family: 'Recursive Mono';
- font-style: normal;
- font-weight: 420;
- font-display: swap;
- src: url(https://fonts.gstatic.com/s/recursive/v23/8vI-7wMr0mhh-RQChyHEH06TlXhq_gukbYrFMk1QuAIcyEwG_X-dpEfaE5YaERmK-CImqvTxvU-MXGX2fSqasNfUlTGZnI14ZeY.woff2)
- format('woff2');
- unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC,
- U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
- }
-
- .tl-container {
- --tl-zoom: 1;
- --tl-scale: calc(1 / var(--tl-zoom));
- --tl-camera-x: 0px;
- --tl-camera-y: 0px;
- --tl-padding: calc(64px * max(1, var(--tl-scale)));
- position: relative;
- top: 0px;
- left: 0px;
- width: 100%;
- height: 100%;
- max-width: 100%;
- max-height: 100%;
- box-sizing: border-box;
- padding: 0px;
- margin: 0px;
- z-index: 100;
- touch-action: none;
- overscroll-behavior: none;
- background-color: var(--tl-background);
- }
-
- .tl-container * {
- user-select: none;
- box-sizing: border-box;
- }
-
- .tl-canvas {
- position: absolute;
- overflow: hidden;
- width: 100%;
- height: 100%;
- touch-action: none;
- pointer-events: all;
- }
-
- .tl-layer {
- position: absolute;
- top: 0;
- left: 0;
- height: 0;
- width: 0;
- contain: layout size;
- transform: scale(var(--tl-zoom)) translate3d(var(--tl-camera-x), var(--tl-camera-y), 0px);
- }
-
- .tl-absolute {
- position: absolute;
- top: 0px;
- left: 0px;
- transform-origin: center center;
- }
-
- .tl-positioned {
- position: absolute;
- top: 0px;
- left: 0px;
- transform-origin: center center;
- pointer-events: none;
- display: flex;
- align-items: center;
- justify-content: center;
- overflow: clip;
- contain: layout size paint;
- }
-
- .tl-positioned-svg {
- width: 100%;
- height: 100%;
- overflow: clip;
- }
-
- .tl-positioned-div {
- position: relative;
- width: 100%;
- height: 100%;
- overflow: hidden;
- padding: var(--tl-padding);
- overflow: clip;
- }
-
- .tl-counter-scaled {
- transform: scale(var(--tl-scale));
- }
-
- .tl-dashed {
- stroke-dasharray: calc(2px * var(--tl-scale)), calc(2px * var(--tl-scale));
- }
-
- .tl-transparent {
- fill: transparent;
- stroke: transparent;
- }
-
- .tl-cursor-ns {
- cursor: ns-resize;
- }
-
- .tl-cursor-ew {
- cursor: ew-resize;
- }
-
- .tl-cursor-nesw {
- cursor: nesw-resize;
- }
-
- .tl-cursor-nwse {
- cursor: nwse-resize;
- }
-
- .tl-corner-handle {
- stroke: var(--tl-selectStroke);
- fill: var(--tl-background);
- stroke-width: calc(1.5px * var(--tl-scale));
- }
-
- .tl-rotate-handle {
- stroke: var(--tl-selectStroke);
- fill: var(--tl-background);
- stroke-width: calc(1.5px * var(--tl-scale));
- cursor: grab;
- }
-
- .tl-binding {
- fill: var(--tl-selectFill);
- stroke: var(--tl-selectStroke);
- stroke-width: calc(1px * var(--tl-scale));
- pointer-events: none;
- }
-
- .tl-selected {
- fill: transparent;
- stroke: var(--tl-selectStroke);
- stroke-width: calc(1.5px * var(--tl-scale));
- pointer-events: none;
- }
-
- .tl-hovered {
- fill: transparent;
- stroke: var(--tl-selectStroke);
- stroke-width: calc(1.5px * var(--tl-scale));
- pointer-events: none;
- }
-
- .tl-bounds {
- pointer-events: none;
- }
-
- .tl-bounds-center {
- fill: transparent;
- stroke: var(--tl-selectStroke);
- stroke-width: calc(1.5px * var(--tl-scale));
- }
-
- .tl-bounds-bg {
- stroke: none;
- fill: var(--tl-selectFill);
- pointer-events: all;
- }
-
- .tl-brush {
- fill: var(--tl-brushFill);
- stroke: var(--tl-brushStroke);
- stroke-width: calc(1px * var(--tl-scale));
- pointer-events: none;
- }
-
- .tl-dot {
- fill: var(--tl-background);
- stroke: var(--tl-foreground);
- stroke-width: 2px;
- }
-
- .tl-handle {
- pointer-events: all;
- }
-
- .tl-handle:hover .tl-handle-bg {
- fill: var(--tl-selectFill);
- }
-
- .tl-handle:hover .tl-handle-bg > * {
- stroke: var(--tl-selectFill);
- }
-
- .tl-handle:active .tl-handle-bg {
- fill: var(--tl-selectFill);
- }
-
- .tl-handle:active .tl-handle-bg > * {
- stroke: var(--tl-selectFill);
- }
-
- .tl-handle {
- fill: var(--tl-background);
- stroke: var(--tl-selectStroke);
- stroke-width: 1.5px;
- }
-
- .tl-handle-bg {
- fill: transparent;
- stroke: none;
- pointer-events: all;
- r: calc(20px / max(1, var(--tl-zoom)));
- }
-
- .tl-binding-indicator {
- stroke-width: calc(3px * var(--tl-scale));
- fill: var(--tl-selectFill);
- stroke: var(--tl-selected);
- }
-
- .tl-centered-g {
- transform: translate(var(--tl-padding), var(--tl-padding));
- }
-
- .tl-current-parent > *[data-shy='true'] {
- opacity: 1;
- }
-
- .tl-binding {
- fill: none;
- stroke: var(--tl-selectStroke);
- stroke-width: calc(2px * var(--tl-scale));
- }
- `
-
- export function useTLTheme(theme?: Partial<TLTheme>) {
- const tltheme = React.useMemo<TLTheme>(
- () => ({
- ...defaultTheme,
- ...theme,
- }),
- [theme]
- )
-
- useTheme('tl', tltheme)
-
- useStyle('tl-canvas', tlcss)
- }
|