Skip to content

Commit e1fc257

Browse files
committed
Merge branch '96-custom-animation-time'
2 parents c59df88 + 686b984 commit e1fc257

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ The only *required* value is `data` (although you will need to provide a `setDat
112112
| `enableClipboard` | `boolean\|CopyFunction` | `true` | Whether or not to enable the "Copy to clipboard" button in the UI. If a function is provided, `true` is assumed and this function will be run whenever an item is copied. |
113113
| `indent` | `number` | `3` | Specify the amount of indentation for each level of nesting in the displayed data. |
114114
| `collapse` | `boolean\|number\|FilterFunction` | `false` | Defines which nodes of the JSON tree will be displayed "opened" in the UI on load. If `boolean`, it'll be either all or none. A `number` specifies a nesting depth after which nodes will be closed. For more fine control a function can be provided — see [Filter functions](#filter-functions). |
115+
| `collapseAnimationTime` | `number` | `500` | Time (in milliseconds) for the transition animation when collapsing collection nodes. |
115116
| `restrictEdit` | `boolean\|FilterFunction` | `false` | If `true`, no editing is permitted. A function can be provided for more specificity — see [Filter functions](#filter-functions) |
116117
| `restrictDelete` | `boolean\|FilterFunction` | `false` | As with `restrictEdit` but for deletion |
117118
| `restrictAdd` | `boolean\|FilterFunction` | `false` | As with `restrictEdit` but for adding new properties |

demo/src/App.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ interface AppState {
5151
rootName: string
5252
indent: number
5353
collapseLevel: number
54+
collapseTime: number
5455
showCount: 'Yes' | 'No' | 'When closed'
5556
theme: ThemeName | Theme
5657
allowEdit: boolean
@@ -75,6 +76,7 @@ function App() {
7576
rootName: dataDefinition.rootName ?? 'data',
7677
indent: 3,
7778
collapseLevel: dataDefinition.collapse ?? 2,
79+
collapseTime: 500,
7880
showCount: 'When closed',
7981
theme: 'default',
8082
allowEdit: true,
@@ -116,6 +118,7 @@ function App() {
116118
theme,
117119
indent,
118120
collapseLevel,
121+
collapseTime,
119122
showCount,
120123
showIndices,
121124
sortKeys,
@@ -330,6 +333,7 @@ function App() {
330333
}
331334
showErrorMessages={dataDefinition.showErrorMessages}
332335
collapse={collapseLevel}
336+
collapseAnimationTime={collapseTime}
333337
showCollectionCount={
334338
showCount === 'Yes' ? true : showCount === 'When closed' ? 'when-closed' : false
335339
}
@@ -477,6 +481,20 @@ function App() {
477481
</NumberInputStepper>
478482
</NumberInput>
479483
</HStack>
484+
<HStack className="inputRow">
485+
<FormLabel className="labelWidth" textAlign="right">
486+
Collapse animation time
487+
</FormLabel>
488+
<NumberInput
489+
id="collapseTime"
490+
className="inputWidth"
491+
min={0}
492+
value={collapseTime}
493+
onChange={(value) => updateState({ collapseTime: Number(value) })}
494+
>
495+
<NumberInputField />
496+
</NumberInput>
497+
</HStack>
480498
<HStack className="inputRow">
481499
<FormLabel className="labelWidth" textAlign="right">
482500
Indent level

src/CollectionNode.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export const CollectionNode: React.FC<CollectionNodeProps> = (props) => {
3232
onDelete,
3333
canDragOnto,
3434
collapseFilter,
35+
collapseAnimationTime,
3536
onMove,
3637
enableClipboard,
3738
searchFilter,
@@ -145,7 +146,7 @@ export const CollectionNode: React.FC<CollectionNodeProps> = (props) => {
145146
hasBeenOpened.current = true
146147
setCollapsed(!collapsed)
147148
setCollapseState(null)
148-
setTimeout(() => setIsAnimating(false), 500)
149+
setTimeout(() => setIsAnimating(false), collapseAnimationTime)
149150
}
150151
}
151152

src/JsonEditor.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const Editor: React.FC<JsonEditorProps> = ({
3737
enableClipboard = true,
3838
indent = 3,
3939
collapse = false,
40+
collapseAnimationTime = 500,
4041
showCollectionCount = true,
4142
restrictEdit = false,
4243
restrictDelete = false,
@@ -72,6 +73,14 @@ const Editor: React.FC<JsonEditorProps> = ({
7273

7374
const [data, setData] = useData<JsonData>({ setData: srcSetData, data: srcData })
7475

76+
const docRoot = document.querySelector(':root') as HTMLElement
77+
const transitionTime = getComputedStyle(document.documentElement).getPropertyValue(
78+
'--jer-expand-transition-time'
79+
)
80+
if (parseFloat(transitionTime) * 1000 !== collapseAnimationTime) {
81+
docRoot?.style.setProperty('--jer-expand-transition-time', `${collapseAnimationTime / 1000}s`)
82+
}
83+
7584
useEffect(() => {
7685
const debounce = setTimeout(() => setDebouncedSearchText(searchText), searchDebounceTime)
7786
return () => clearTimeout(debounce)
@@ -249,6 +258,7 @@ const Editor: React.FC<JsonEditorProps> = ({
249258
onMove,
250259
showCollectionCount,
251260
collapseFilter,
261+
collapseAnimationTime,
252262
restrictEditFilter,
253263
restrictDeleteFilter,
254264
restrictAddFilter,

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface JsonEditorProps {
2222
id?: string
2323
indent?: number
2424
collapse?: boolean | number | FilterFunction
25+
collapseAnimationTime?: number // ms
2526
showCollectionCount?: boolean | 'when-closed'
2627
restrictEdit?: boolean | FilterFunction
2728
restrictDelete?: boolean | FilterFunction
@@ -192,6 +193,7 @@ interface BaseNodeProps {
192193
export interface CollectionNodeProps extends BaseNodeProps {
193194
data: CollectionData
194195
collapseFilter: FilterFunction
196+
collapseAnimationTime: number
195197
onAdd: InternalUpdateFunction
196198
keySort: boolean | CompareFunction
197199
showArrayIndices: boolean

0 commit comments

Comments
 (0)