@liberfi.io/ui-tradingview 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +232 -0
- package/dist/index.d.mts +938 -0
- package/dist/index.d.ts +938 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +41 -0
package/README.md
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
# @liberfi.io/ui-tradingview
|
|
2
|
+
|
|
3
|
+
TradingView K-line charting component for Liberfi React SDK. This package wraps the TradingView Charting Library with jotai-based state management and multi-instance support, providing a composable `TradingView` component with injectable DataFeed interface.
|
|
4
|
+
|
|
5
|
+
## Design Philosophy
|
|
6
|
+
|
|
7
|
+
- **Multi-instance isolation** — Every chart instance is keyed by a `storageId` (prefix). State atoms use `atomFamily(prefix)` so multiple charts coexist without creating extra jotai Providers or stores.
|
|
8
|
+
- **No RxJS** — All reactive state lives in jotai atoms. Internal bridge-level events (symbol changes, crosshair moves) use a lightweight `EventEmitter`.
|
|
9
|
+
- **Injectable DataFeed** — The consumer provides the `ITvChartDataFeedModule` implementation and the TradingView `Widget` constructor. This package does not bundle the charting library or data-fetching logic.
|
|
10
|
+
- **Composable toolbar** — The built-in toolbar ships default controls (resolutions, kline style, indicators, settings, fullscreen, snapshot) that can be individually toggled or fully replaced via slots.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pnpm add @liberfi.io/ui-tradingview
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
### Peer Dependencies
|
|
19
|
+
|
|
20
|
+
The consumer must provide:
|
|
21
|
+
|
|
22
|
+
| Dependency | Version |
|
|
23
|
+
| ----------- | --------- |
|
|
24
|
+
| `react` | >= 18 |
|
|
25
|
+
| `react-dom` | >= 18 |
|
|
26
|
+
| `jotai` | >= 2.15.1 |
|
|
27
|
+
|
|
28
|
+
The TradingView Charting Library static files must be hosted by the consumer and the `Widget` constructor passed as a prop.
|
|
29
|
+
|
|
30
|
+
## API Reference
|
|
31
|
+
|
|
32
|
+
### Components
|
|
33
|
+
|
|
34
|
+
#### `TradingView`
|
|
35
|
+
|
|
36
|
+
Main entry component. Accepts chart configuration, the injected widget constructor, and toolbar options.
|
|
37
|
+
|
|
38
|
+
```tsx
|
|
39
|
+
<TradingView
|
|
40
|
+
initConfig={config}
|
|
41
|
+
widgetConstructor={window.TradingView.widget}
|
|
42
|
+
libraryPath="/static/charting_library/"
|
|
43
|
+
toolbar={{ showSnapshot: true, showFullscreen: true }}
|
|
44
|
+
loadingOverlay={<Spinner />}
|
|
45
|
+
onReady={() => console.log("chart ready")}
|
|
46
|
+
/>
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
**Props:**
|
|
50
|
+
|
|
51
|
+
| Prop | Type | Default | Description |
|
|
52
|
+
| ------------------- | ---------------------------------- | ---------------------------------------------- | ---------------------------------- |
|
|
53
|
+
| `initConfig` | `TvChartConfig` | (required) | Chart configuration object |
|
|
54
|
+
| `widgetConstructor` | `WidgetConstructor` | (required) | TradingView Widget constructor |
|
|
55
|
+
| `libraryPath` | `string` | `"/static/charting_library/"` | Path to library static files |
|
|
56
|
+
| `customCssUrl` | `string` | `"/static/charting_library/custom-styles.css"` | Custom CSS URL |
|
|
57
|
+
| `toolbar` | `TradingViewToolbarProps \| false` | `{}` | Toolbar config, or `false` to hide |
|
|
58
|
+
| `loadingOverlay` | `ReactNode` | - | Overlay shown while loading |
|
|
59
|
+
| `onReady` | `() => void` | - | Called when widget is ready |
|
|
60
|
+
| `className` | `string` | - | Outer container class |
|
|
61
|
+
|
|
62
|
+
#### `TradingViewToolbar`
|
|
63
|
+
|
|
64
|
+
Composable toolbar with slot-based customization.
|
|
65
|
+
|
|
66
|
+
| Prop | Type | Default | Description |
|
|
67
|
+
| ---------------------- | ----------- | ------- | ------------------------------- |
|
|
68
|
+
| `showResolutions` | `boolean` | `true` | Show resolution buttons |
|
|
69
|
+
| `showKlineStyleSelect` | `boolean` | `true` | Show kline style selector |
|
|
70
|
+
| `showOpenIndicator` | `boolean` | `true` | Show indicator button |
|
|
71
|
+
| `showOpenSettings` | `boolean` | `true` | Show settings button |
|
|
72
|
+
| `showFullscreen` | `boolean` | `true` | Show fullscreen toggle |
|
|
73
|
+
| `showSnapshot` | `boolean` | `true` | Show snapshot button |
|
|
74
|
+
| `prefix` | `ReactNode` | - | Slot before default items |
|
|
75
|
+
| `suffix` | `ReactNode` | - | Slot after default items |
|
|
76
|
+
| `children` | `ReactNode` | - | Override default items entirely |
|
|
77
|
+
|
|
78
|
+
#### Sub-components
|
|
79
|
+
|
|
80
|
+
- `TradingViewResolutions` — Resolution toggle buttons
|
|
81
|
+
- `TradingViewKlineStyleSelect` — K-line style dropdown
|
|
82
|
+
- `TradingViewOpenIndicator` — "Indicators" button
|
|
83
|
+
- `TradingViewOpenSettings` — "Settings" button
|
|
84
|
+
- `TradingViewFullscreen` — Fullscreen toggle
|
|
85
|
+
- `TradingViewSnapshot` — Screenshot download button
|
|
86
|
+
- `TradingViewAreaTitle` — Displays symbol/resolution for a chart area
|
|
87
|
+
|
|
88
|
+
### Hooks
|
|
89
|
+
|
|
90
|
+
| Hook | Return | Description |
|
|
91
|
+
| ------------------------ | --------------------------- | ---------------------------------------- |
|
|
92
|
+
| `useChartManager()` | `ChartManager` | Access the chart manager instance |
|
|
93
|
+
| `useActiveAreaManager()` | `ChartAreaManager \| null` | Active chart area manager |
|
|
94
|
+
| `useSymbolInfo()` | `LibrarySymbolInfo \| null` | Resolved symbol info for active area |
|
|
95
|
+
| `useTvChartContext()` | `TvChartContextValue` | Full context (manager, settings, prefix) |
|
|
96
|
+
| `useTvChartManager()` | `ChartManager` | Shortcut for context.chartManager |
|
|
97
|
+
| `useTvChartPrefix()` | `string` | Current instance prefix (storageId) |
|
|
98
|
+
|
|
99
|
+
### Types & Enums
|
|
100
|
+
|
|
101
|
+
| Type / Enum | Description |
|
|
102
|
+
| ------------------------ | -------------------------------------------------- |
|
|
103
|
+
| `TvChartConfig` | Configuration passed to `initConfig` |
|
|
104
|
+
| `TvChartResolution` | `"1s" \| "5s" \| ... \| "1d"` |
|
|
105
|
+
| `TvChartKlineStyle` | Candles, Bars, Line, Area, HeikenAshi, etc. |
|
|
106
|
+
| `TvChartLayout` | Layout presets (1A, 2A, 3A, etc.) |
|
|
107
|
+
| `TvChartTheme` | `"light" \| "dark"` |
|
|
108
|
+
| `TvChartFeature` | Feature flags (MultiCharts, VolumeForceOverlay...) |
|
|
109
|
+
| `ITvChartDataFeedModule` | Interface consumers implement for data fetching |
|
|
110
|
+
| `ITvChartSymbolResolver` | Interface for symbol info resolution |
|
|
111
|
+
| `ChartAreaState` | Shape of per-area state in atoms |
|
|
112
|
+
|
|
113
|
+
### Atoms
|
|
114
|
+
|
|
115
|
+
All atoms are `atomFamily` instances keyed by `prefix` (storageId). Advanced consumers can read them directly:
|
|
116
|
+
|
|
117
|
+
- `chartLoadingFamily(prefix)` — `boolean`
|
|
118
|
+
- `chartFullscreenFamily(prefix)` — `boolean`
|
|
119
|
+
- `chartSelectedIndexFamily(prefix)` — `number`
|
|
120
|
+
- `chartPinnedResolutionsFamily(prefix)` — `TvChartResolution[]`
|
|
121
|
+
- `chartAreasFamily(prefix)` — `ChartAreaState[]`
|
|
122
|
+
- `settingsThemeFamily(prefix)` — `string`
|
|
123
|
+
- `settingsLayoutFamily(prefix)` — `TvChartLayout`
|
|
124
|
+
- `widgetReadyFamily(prefix)` — `boolean`
|
|
125
|
+
- `settingsDataFamily(prefix)` — Derived atom for auto-save
|
|
126
|
+
|
|
127
|
+
### Constants
|
|
128
|
+
|
|
129
|
+
- `ALL_TV_CHART_RESOLUTIONS` — All supported resolutions
|
|
130
|
+
- `DEFAULT_TV_CHART_RESOLUTIONS` — Default pinned resolutions
|
|
131
|
+
- `SUPPORTED_TV_CHART_LAYOUTS` — All layout presets
|
|
132
|
+
- `TV_CHART_THEME_COLORS` — Default color palette
|
|
133
|
+
|
|
134
|
+
## Usage Examples
|
|
135
|
+
|
|
136
|
+
### Basic Usage
|
|
137
|
+
|
|
138
|
+
```tsx
|
|
139
|
+
import { TradingView, type TvChartConfig } from "@liberfi.io/ui-tradingview";
|
|
140
|
+
|
|
141
|
+
const config: TvChartConfig = {
|
|
142
|
+
storageId: "kline",
|
|
143
|
+
tickerSymbol: "solana/So11111111111111111111111111111111111111112",
|
|
144
|
+
datafeed: myDataFeedModule,
|
|
145
|
+
resolution: "1m",
|
|
146
|
+
layout: "1A",
|
|
147
|
+
chartType: "TradingView",
|
|
148
|
+
kLineStyle: 1,
|
|
149
|
+
theme: "dark",
|
|
150
|
+
timezone: "Etc/UTC",
|
|
151
|
+
locale: "en",
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
function App() {
|
|
155
|
+
return (
|
|
156
|
+
<TradingView
|
|
157
|
+
initConfig={config}
|
|
158
|
+
widgetConstructor={window.TradingView.widget}
|
|
159
|
+
libraryPath="/static/charting_library/"
|
|
160
|
+
/>
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Multiple Instances
|
|
166
|
+
|
|
167
|
+
```tsx
|
|
168
|
+
function MultiChart() {
|
|
169
|
+
return (
|
|
170
|
+
<div className="flex gap-4">
|
|
171
|
+
<TradingView
|
|
172
|
+
initConfig={{ ...config, storageId: "chart-1" }}
|
|
173
|
+
widgetConstructor={window.TradingView.widget}
|
|
174
|
+
/>
|
|
175
|
+
<TradingView
|
|
176
|
+
initConfig={{ ...config, storageId: "chart-2" }}
|
|
177
|
+
widgetConstructor={window.TradingView.widget}
|
|
178
|
+
/>
|
|
179
|
+
</div>
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Custom Toolbar
|
|
185
|
+
|
|
186
|
+
```tsx
|
|
187
|
+
<TradingView
|
|
188
|
+
initConfig={config}
|
|
189
|
+
widgetConstructor={window.TradingView.widget}
|
|
190
|
+
toolbar={{
|
|
191
|
+
showSnapshot: false,
|
|
192
|
+
prefix: <MyCustomSymbolSelector />,
|
|
193
|
+
suffix: <MyCustomActions />,
|
|
194
|
+
}}
|
|
195
|
+
/>
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### Imperative API
|
|
199
|
+
|
|
200
|
+
```tsx
|
|
201
|
+
import { useRef } from "react";
|
|
202
|
+
import { TradingView, type TvChartHandle } from "@liberfi.io/ui-tradingview";
|
|
203
|
+
|
|
204
|
+
function App() {
|
|
205
|
+
const ref = useRef<TvChartHandle>(null);
|
|
206
|
+
|
|
207
|
+
const handleToggleTheme = () => {
|
|
208
|
+
const current = ref.current?.theme();
|
|
209
|
+
ref.current?.setTheme(current === "dark" ? "light" : "dark");
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
return (
|
|
213
|
+
<>
|
|
214
|
+
<button onClick={handleToggleTheme}>Toggle Theme</button>
|
|
215
|
+
<TradingView
|
|
216
|
+
ref={ref}
|
|
217
|
+
initConfig={config}
|
|
218
|
+
widgetConstructor={window.TradingView.widget}
|
|
219
|
+
/>
|
|
220
|
+
</>
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## Future Improvements
|
|
226
|
+
|
|
227
|
+
- Add `TradingViewAreaTitle` injection into TradingView widget iframe for multi-chart layouts
|
|
228
|
+
- Support consumer-provided color palettes via props
|
|
229
|
+
- Add built-in `ITvChartDataFeedModule` implementation for common REST/WebSocket patterns
|
|
230
|
+
- Support chart comparison mode
|
|
231
|
+
- Add keyboard shortcut customization
|
|
232
|
+
- Expose drawing tools API
|