Skip to content

Commit b828bf0

Browse files
authored
fix(tldraw): export TldrawUiTranslationProvider and dedupe missing-translation warning (tldraw#8909)
In order to let consumers render components like `TldrawSelectionForeground` without pulling in the full `TldrawUiContextProvider` (and its icon preloading), this PR promotes `TldrawUiTranslationProvider`, `AssetUrlsProvider`, and `useAssetUrls` from `@internal` to `@public`. It also deduplicates the "No translation messages found" warning so it only logs once per session instead of once per component that calls `useTranslation`, and rewords the message to point developers at `TldrawUiTranslationProvider` as an alternative. Closes tldraw#6236 Consumers can now do: ```tsx <AssetUrlsProvider assetUrls={...}> <TldrawUiTranslationProvider locale="en"> <TldrawSelectionForeground ... /> </TldrawUiTranslationProvider> </AssetUrlsProvider> ``` ### Change type - [x] `bugfix` ### Test plan 1. Render a component that uses `useTranslation` (e.g. `TldrawSelectionForeground`) outside of `TldrawUiContextProvider`. 2. Confirm the "No translation messages found" warning prints **once**, not once per consumer. 3. Wrap the same component in `<AssetUrlsProvider><TldrawUiTranslationProvider locale="en">` and confirm the warning is silenced and translations resolve. - [ ] Unit tests - [ ] End to end tests ### Release notes - Export `TldrawUiTranslationProvider`, `AssetUrlsProvider`, and `useAssetUrls` as public API so `TldrawSelectionForeground` and similar components can be used without `TldrawUiContextProvider`. - Only log the missing-translation warning once per session instead of once per `useTranslation` consumer. ### API changes - Changed `TldrawUiTranslationProvider` from `@internal` to `@public @react`. - Changed `AssetUrlsProvider` from `@internal` to `@public @react`. - Changed `useAssetUrls` from `@internal` to `@public`. ### Code changes | Section | LOC change | | --------------- | ---------- | | Core code | +18 / -6 | | Automated files | +3 / -3 |
1 parent 6dbd8b4 commit b828bf0

3 files changed

Lines changed: 18 additions & 8 deletions

File tree

packages/tldraw/api-report.api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ export const ASPECT_RATIO_TO_VALUE: Record<ASPECT_RATIO_OPTION, number>;
478478
// @public (undocumented)
479479
export function AssetToolbarItem(): JSX.Element;
480480

481-
// @internal (undocumented)
481+
// @public
482482
export function AssetUrlsProvider({ assetUrls, children }: {
483483
assetUrls: TLUiAssetUrls;
484484
children: React.ReactNode;
@@ -4364,7 +4364,7 @@ export interface TldrawUiTooltipProviderProps {
43644364
children: React_3.ReactNode;
43654365
}
43664366

4367-
// @internal
4367+
// @public
43684368
export function TldrawUiTranslationProvider({ overrides, locale, children }: TLUiTranslationProviderProps): JSX.Element;
43694369

43704370
// @public (undocumented)
@@ -6309,7 +6309,7 @@ export function useA11y(): TLUiA11yContextType;
63096309
// @public (undocumented)
63106310
export function useActions(): TLUiActionsContextType;
63116311

6312-
// @internal (undocumented)
6312+
// @public (undocumented)
63136313
export function useAssetUrls(): TLUiAssetUrls;
63146314

63156315
// @public (undocumented)

packages/tldraw/src/lib/ui/context/asset-urls.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ type UiAssetUrlsContextType = TLUiAssetUrls | null
77

88
const AssetUrlsContext = createContext<UiAssetUrlsContextType>(null)
99

10-
/** @internal */
10+
/**
11+
* Provides asset URLs (icons, fonts, translations, embed icons) to the editor's UI.
12+
* Required when using `TldrawUiTranslationProvider` without `TldrawUiContextProvider`.
13+
*
14+
* @public @react
15+
*/
1116
export function AssetUrlsProvider({
1217
assetUrls,
1318
children,
@@ -37,7 +42,7 @@ export function AssetUrlsProvider({
3742
return <AssetUrlsContext.Provider value={assetUrls}>{children}</AssetUrlsContext.Provider>
3843
}
3944

40-
/** @internal */
45+
/** @public */
4146
export function useAssetUrls() {
4247
const assetUrls = useContext(AssetUrlsContext)
4348
if (!assetUrls) {

packages/tldraw/src/lib/ui/hooks/useTranslation/useTranslation.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { warnOnce } from '@tldraw/editor'
12
import * as React from 'react'
23
import { useAssetUrls } from '../../context/asset-urls'
34
import { DEFAULT_TRANSLATION } from './defaultTranslation'
@@ -36,9 +37,11 @@ export function useCurrentTranslation() {
3637
}
3738

3839
/**
39-
* Provides a translation context to the editor.
40+
* Provides a translation context to the editor. Wrap this around components that use
41+
* `useTranslation` (such as `TldrawSelectionForeground`) when you don't want to use the
42+
* full `TldrawUiContextProvider`. Must be rendered inside an `AssetUrlsProvider`.
4043
*
41-
* @internal
44+
* @public @react
4245
*/
4346
export function TldrawUiTranslationProvider({
4447
overrides,
@@ -115,7 +118,9 @@ export function useTranslation() {
115118

116119
React.useEffect(() => {
117120
if (!translation?.messages) {
118-
console.warn('No translation messages found, falling back to default translation.')
121+
warnOnce(
122+
'No translation messages found, falling back to default translation. Wrap your app in <TldrawUiContextProvider>, or in both <AssetUrlsProvider> and <TldrawUiTranslationProvider>, to provide translations.'
123+
)
119124
}
120125
}, [translation?.messages])
121126

0 commit comments

Comments
 (0)