Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add floaty window example #2250

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions apps/examples/src/examples/FloatyExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Tldraw, Vec2d, useContainer, useEditor } from '@tldraw/tldraw'
import '@tldraw/tldraw/tldraw.css'
import { useEffect } from 'react'

export default function FloatyExample() {
return (
<div className="tldraw__editor">
<Tldraw persistenceKey="tldraw_floaty_example">
<SneakyFloatyHook />
</Tldraw>
</div>
)
}

function SneakyFloatyHook() {
const editor = useEditor()
const container = useContainer()

useEffect(() => {
if (!window.screenLeft) {
window.screenLeft = window.screenX
window.screenTop = window.screenY
}

let x = window.screenLeft ?? window.screenX
let y = window.screenTop ?? window.screenY

function updatePositions() {
const sx = window.screenLeft ?? window.screenX
const sy = window.screenTop ?? window.screenY

if (sx !== x || sy !== y) {
x = sx
y = sy
editor.setCamera(new Vec2d(-x, -y))
}
}

editor.on('tick', updatePositions)

return () => {
editor.off('tick', updatePositions)
}
}, [editor, container])

return null
}
6 changes: 6 additions & 0 deletions apps/examples/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import CustomUiExample from './examples/CustomUiExample/CustomUiExample'
import ErrorBoundaryExample from './examples/ErrorBoundaryExample/ErrorBoundaryExample'
import ExplodedExample from './examples/ExplodedExample'
import ExternalContentSourcesExample from './examples/ExternalContentSourcesExample'
import FloatyExample from './examples/FloatyExample'
import ForceMobileExample from './examples/ForceBreakpointExample'
import HideUiExample from './examples/HideUiExample'
import MetaExample from './examples/MetaExample'
Expand Down Expand Up @@ -192,6 +193,11 @@ export const allExamples: Example[] = [
path: 'asset-props',
element: <AssetPropsExample />,
},
{
title: 'Floaty window',
path: 'floaty-window',
element: <FloatyExample />,
},
{
title: 'External content sources',
path: 'external-content-sources',
Expand Down
Loading