-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: render tooltip in root (#2210)
- Loading branch information
1 parent
4ccfc3b
commit feebb69
Showing
22 changed files
with
433 additions
and
102 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
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
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,71 @@ | ||
import { subscribeKey as subKey } from 'valtio/vanilla/utils' | ||
import { proxy, subscribe as sub } from 'valtio/vanilla' | ||
|
||
// -- Types --------------------------------------------- // | ||
type TriggerRect = { | ||
width: number | ||
height: number | ||
top: number | ||
left: number | ||
} | ||
|
||
export interface TooltipControllerState { | ||
message: string | ||
triggerRect: TriggerRect | ||
open: boolean | ||
variant: 'shade' | 'fill' | ||
} | ||
|
||
type StateKey = keyof TooltipControllerState | ||
|
||
// -- State --------------------------------------------- // | ||
const state = proxy<TooltipControllerState>({ | ||
message: '', | ||
open: false, | ||
triggerRect: { | ||
width: 0, | ||
height: 0, | ||
top: 0, | ||
left: 0 | ||
}, | ||
variant: 'shade' | ||
}) | ||
|
||
// -- Controller ---------------------------------------- // | ||
export const TooltipController = { | ||
state, | ||
|
||
subscribe(callback: (newState: TooltipControllerState) => void) { | ||
return sub(state, () => callback(state)) | ||
}, | ||
|
||
subscribeKey<K extends StateKey>(key: K, callback: (value: TooltipControllerState[K]) => void) { | ||
return subKey(state, key, callback) | ||
}, | ||
|
||
showTooltip({ | ||
message, | ||
triggerRect, | ||
variant | ||
}: { | ||
message: string | ||
triggerRect: TriggerRect | ||
variant: 'shade' | 'fill' | ||
}) { | ||
state.open = true | ||
state.message = message | ||
state.triggerRect = triggerRect | ||
state.variant = variant | ||
}, | ||
|
||
hide() { | ||
state.open = false | ||
state.message = '' | ||
state.triggerRect = { | ||
width: 0, | ||
height: 0, | ||
top: 0, | ||
left: 0 | ||
} | ||
} | ||
} |
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,44 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { TooltipController } from '../../index.js' | ||
|
||
// -- Constants ---------------------------------------------------------------- | ||
const mockTriggerRect = { | ||
width: 100, | ||
height: 100, | ||
top: 100, | ||
left: 100 | ||
} | ||
|
||
const mockTooltip = { | ||
message: 'Hello, World!', | ||
triggerRect: mockTriggerRect, | ||
variant: 'shade' as 'shade' | 'fill' | ||
} | ||
|
||
// -- Tests -------------------------------------------------------------------- | ||
describe('TooltipController', () => { | ||
it('should have valid default state', () => { | ||
expect(TooltipController.state).toEqual({ | ||
message: '', | ||
open: false, | ||
triggerRect: { | ||
width: 0, | ||
height: 0, | ||
top: 0, | ||
left: 0 | ||
}, | ||
variant: 'shade' | ||
}) | ||
}) | ||
|
||
it('should show the tooltip as expected', () => { | ||
TooltipController.showTooltip(mockTooltip) | ||
expect(TooltipController.state.open).toEqual(true) | ||
expect(TooltipController.state.message).toEqual('Hello, World!') | ||
}) | ||
|
||
it('should hide the tooltip as expected', () => { | ||
TooltipController.hide() | ||
expect(TooltipController.state.open).toEqual(false) | ||
}) | ||
}) |
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
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
Oops, something went wrong.