sefaria-ui-components 0.1.0
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/LICENSE +21 -0
- package/README.md +246 -0
- package/dist/index.cjs +8712 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +238 -0
- package/dist/index.d.ts +238 -0
- package/dist/index.js +8697 -0
- package/dist/index.js.map +1 -0
- package/package.json +75 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Brett Lockspeiser
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
# Sefaria UI Components
|
|
2
|
+
|
|
3
|
+
A collection of reusable React components for building Torah study applications, powered by the Sefaria API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install sefaria-ui-components
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
or with yarn:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
yarn add sefaria-ui-components
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Quick Start
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import { TextBlock } from 'sefaria-ui-components';
|
|
21
|
+
|
|
22
|
+
function App() {
|
|
23
|
+
return (
|
|
24
|
+
<TextBlock
|
|
25
|
+
ref="Genesis 1:1"
|
|
26
|
+
sefariaData={apiResponse}
|
|
27
|
+
/>
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Components
|
|
33
|
+
|
|
34
|
+
### TextBlock
|
|
35
|
+
|
|
36
|
+
Displays a Sefaria text source with a colored border indicating its category. Supports loading states, single verses, verse ranges, and chapters.
|
|
37
|
+
|
|
38
|
+
#### Props
|
|
39
|
+
|
|
40
|
+
| Prop | Type | Description |
|
|
41
|
+
|------|------|-------------|
|
|
42
|
+
| `ref` | `string` | Sefaria citation reference (e.g., "Genesis 1:1", "Berakhot 2a") |
|
|
43
|
+
| `sefariaData` | `SefariaTextResponse` | Data from Sefaria Text API. If provided, renders full text. |
|
|
44
|
+
| `showFollowup` | `boolean` | Show the "Follow up" button with action menu |
|
|
45
|
+
| `onEvent` | `EventHandler<TextBlockEvent>` | Event handler for component events |
|
|
46
|
+
| `className` | `string` | Additional CSS class name |
|
|
47
|
+
| `style` | `CSSProperties` | Additional inline styles |
|
|
48
|
+
|
|
49
|
+
#### Basic Usage
|
|
50
|
+
|
|
51
|
+
```tsx
|
|
52
|
+
import { TextBlock } from 'sefaria-ui-components';
|
|
53
|
+
|
|
54
|
+
// Loading state - shows "Loading..." until data arrives
|
|
55
|
+
<TextBlock ref="Genesis 1:1" />
|
|
56
|
+
|
|
57
|
+
// With data - renders the full text
|
|
58
|
+
<TextBlock
|
|
59
|
+
ref="Genesis 1:1"
|
|
60
|
+
sefariaData={fetchedData}
|
|
61
|
+
/>
|
|
62
|
+
|
|
63
|
+
// With followup button
|
|
64
|
+
<TextBlock
|
|
65
|
+
ref="Genesis 1:1"
|
|
66
|
+
sefariaData={fetchedData}
|
|
67
|
+
showFollowup
|
|
68
|
+
/>
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
#### Fetching Data from Sefaria API
|
|
72
|
+
|
|
73
|
+
```tsx
|
|
74
|
+
import { TextBlock, type SefariaTextResponse } from 'sefaria-ui-components';
|
|
75
|
+
import { useState, useEffect } from 'react';
|
|
76
|
+
|
|
77
|
+
function SefariaText({ citation }: { citation: string }) {
|
|
78
|
+
const [data, setData] = useState<SefariaTextResponse | null>(null);
|
|
79
|
+
|
|
80
|
+
useEffect(() => {
|
|
81
|
+
const encoded = encodeURIComponent(citation);
|
|
82
|
+
fetch(`https://www.sefaria.org/api/v3/texts/${encoded}`)
|
|
83
|
+
.then(res => res.json())
|
|
84
|
+
.then(json => {
|
|
85
|
+
// Transform API v3 response
|
|
86
|
+
const englishVersion = json.versions?.find(
|
|
87
|
+
(v: { language: string }) => v.language === 'en'
|
|
88
|
+
);
|
|
89
|
+
setData({
|
|
90
|
+
ref: json.ref,
|
|
91
|
+
heRef: json.heRef,
|
|
92
|
+
text: englishVersion?.text ?? json.text,
|
|
93
|
+
he: json.he,
|
|
94
|
+
sections: json.sections,
|
|
95
|
+
toSections: json.toSections,
|
|
96
|
+
primary_category: json.primary_category,
|
|
97
|
+
type: json.type,
|
|
98
|
+
categories: json.categories,
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
}, [citation]);
|
|
102
|
+
|
|
103
|
+
return <TextBlock ref={citation} sefariaData={data ?? undefined} />;
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
#### Handling Events
|
|
108
|
+
|
|
109
|
+
TextBlock emits events for user interactions:
|
|
110
|
+
|
|
111
|
+
```tsx
|
|
112
|
+
import { TextBlock, type TextBlockEvent } from 'sefaria-ui-components';
|
|
113
|
+
|
|
114
|
+
function App() {
|
|
115
|
+
const handleEvent = (event: TextBlockEvent) => {
|
|
116
|
+
switch (event.type) {
|
|
117
|
+
case 'TextBlock:click':
|
|
118
|
+
console.log('Clicked:', event.data.ref);
|
|
119
|
+
break;
|
|
120
|
+
case 'TextBlock:linkClick':
|
|
121
|
+
console.log('Link clicked:', event.data.ref, event.data.href);
|
|
122
|
+
break;
|
|
123
|
+
case 'TextBlock:followup':
|
|
124
|
+
console.log('Followup action:', event.data.action, event.data.ref);
|
|
125
|
+
// Handle followup actions like 'explain', 'summarize', 'translate', etc.
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
return (
|
|
131
|
+
<TextBlock
|
|
132
|
+
ref="Genesis 1:1"
|
|
133
|
+
sefariaData={data}
|
|
134
|
+
showFollowup
|
|
135
|
+
onEvent={handleEvent}
|
|
136
|
+
/>
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
#### Event Types
|
|
142
|
+
|
|
143
|
+
| Event | Data | Description |
|
|
144
|
+
|-------|------|-------------|
|
|
145
|
+
| `TextBlock:click` | `{ ref: string }` | Fired when the text block is clicked |
|
|
146
|
+
| `TextBlock:linkClick` | `{ ref: string, href: string }` | Fired when the citation link is clicked |
|
|
147
|
+
| `TextBlock:followup` | `{ ref: string, action: FollowupAction }` | Fired when a followup action is selected |
|
|
148
|
+
|
|
149
|
+
#### Followup Actions
|
|
150
|
+
|
|
151
|
+
When `showFollowup` is enabled, users can select from these actions:
|
|
152
|
+
|
|
153
|
+
- `explain` - Explain the plain meaning
|
|
154
|
+
- `summarize` - Summarize in bullet points
|
|
155
|
+
- `translate` - Translate to English
|
|
156
|
+
- `suggest_questions` - Suggest follow-up questions
|
|
157
|
+
- `commentary_top` - Show most-cited commentary
|
|
158
|
+
- `commentary_consensus` - Show points of agreement among commentaries
|
|
159
|
+
- `commentary_disagreements` - Show points of disagreement
|
|
160
|
+
- `commentary_unusual` - Show lesser-known commentary
|
|
161
|
+
- `connect_to_life` - Connect to modern life
|
|
162
|
+
- `trace_usage` - Trace how the text is understood in later sources
|
|
163
|
+
|
|
164
|
+
## Types
|
|
165
|
+
|
|
166
|
+
### SefariaTextResponse
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
interface SefariaTextResponse {
|
|
170
|
+
ref?: string;
|
|
171
|
+
heRef?: string;
|
|
172
|
+
text?: unknown;
|
|
173
|
+
he?: unknown;
|
|
174
|
+
sections?: unknown;
|
|
175
|
+
toSections?: unknown;
|
|
176
|
+
primary_category?: unknown;
|
|
177
|
+
type?: unknown;
|
|
178
|
+
categories?: unknown;
|
|
179
|
+
[key: string]: unknown;
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### FollowupAction
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
type FollowupAction =
|
|
187
|
+
| 'explain'
|
|
188
|
+
| 'summarize'
|
|
189
|
+
| 'translate'
|
|
190
|
+
| 'suggest_questions'
|
|
191
|
+
| 'commentary_top'
|
|
192
|
+
| 'commentary_consensus'
|
|
193
|
+
| 'commentary_disagreements'
|
|
194
|
+
| 'commentary_unusual'
|
|
195
|
+
| 'connect_to_life'
|
|
196
|
+
| 'trace_usage';
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Utilities
|
|
200
|
+
|
|
201
|
+
### Followup Prompts
|
|
202
|
+
|
|
203
|
+
Build followup prompts for AI assistants:
|
|
204
|
+
|
|
205
|
+
```tsx
|
|
206
|
+
import {
|
|
207
|
+
buildFollowupUserMessage,
|
|
208
|
+
renderFollowupTemplate,
|
|
209
|
+
FOLLOWUP_TEMPLATES,
|
|
210
|
+
type FollowupAction,
|
|
211
|
+
} from 'sefaria-ui-components';
|
|
212
|
+
|
|
213
|
+
// Get the template for an action
|
|
214
|
+
const template = FOLLOWUP_TEMPLATES['explain'];
|
|
215
|
+
// "Explain in clear language what the plain meaning of {citation} is."
|
|
216
|
+
|
|
217
|
+
// Render with a specific citation
|
|
218
|
+
const prompt = renderFollowupTemplate('explain', 'Genesis 1:1');
|
|
219
|
+
// "Explain in clear language what the plain meaning of Genesis 1:1 is."
|
|
220
|
+
|
|
221
|
+
// Build a full user message (for chat interfaces)
|
|
222
|
+
const message = buildFollowupUserMessage({
|
|
223
|
+
action: 'explain',
|
|
224
|
+
citation: 'Genesis 1:1',
|
|
225
|
+
includeRePrefix: false,
|
|
226
|
+
});
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## Styling
|
|
230
|
+
|
|
231
|
+
TextBlock uses inline styles with sensible defaults. The left border color is automatically determined by the text's category (Tanakh, Talmud, Midrash, etc.).
|
|
232
|
+
|
|
233
|
+
To customize styles, use the `className` and `style` props:
|
|
234
|
+
|
|
235
|
+
```tsx
|
|
236
|
+
<TextBlock
|
|
237
|
+
ref="Genesis 1:1"
|
|
238
|
+
sefariaData={data}
|
|
239
|
+
className="my-custom-class"
|
|
240
|
+
style={{ maxWidth: '600px' }}
|
|
241
|
+
/>
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## License
|
|
245
|
+
|
|
246
|
+
MIT
|