Skip to content

Commit 695dfa9

Browse files
committed
Fixing HiddenComponent to work with multiple levels of nestsing
1 parent ef84111 commit 695dfa9

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

super_editor/lib/src/core/document_layout.dart

+40-10
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,56 @@ class DocumentLayoutEditable implements Editable {
3131
void onTransactionStart() {}
3232
}
3333

34+
/// Wraps document components to hide them when [hiding] is true.
35+
///
36+
/// A hidden component is not visible but has its state maintained.
37+
/// This widget will hide its child if [hiding] is true OR it has
38+
/// an ancestor [HiddenComponent] widget with [hiding] set to true.
3439
class HiddenComponent extends StatelessWidget {
35-
const HiddenComponent({super.key, required this.child, this.hiding = true});
40+
const HiddenComponent({
41+
super.key,
42+
required this.hiding,
43+
required this.child,
44+
});
3645

3746
final Widget child;
3847
final bool hiding;
3948

4049
@override
4150
Widget build(BuildContext context) {
42-
return Visibility(
43-
visible: !hiding,
44-
maintainAnimation: false,
45-
maintainInteractivity: false,
46-
maintainSemantics: false,
47-
maintainSize: false,
48-
maintainState: true,
49-
child: child,
51+
final computedHiding =
52+
hiding || (context.dependOnInheritedWidgetOfExactType<_HiddenComponentScope>()?.hiding ?? false);
53+
return _HiddenComponentScope(
54+
hiding: computedHiding,
55+
child: Visibility(
56+
visible: !computedHiding,
57+
maintainAnimation: false,
58+
maintainInteractivity: false,
59+
maintainSemantics: false,
60+
maintainSize: false,
61+
maintainState: true,
62+
child: child,
63+
),
5064
);
5165
}
5266
}
5367

68+
class _HiddenComponentScope extends InheritedWidget {
69+
const _HiddenComponentScope({required super.child, this.hiding = true});
70+
71+
final bool hiding;
72+
73+
@override
74+
bool updateShouldNotify(_HiddenComponentScope oldWidget) {
75+
return oldWidget.hiding != hiding;
76+
}
77+
78+
static _HiddenComponentScope of(BuildContext context) => _HiddenComponentScope.maybeOf(context)!;
79+
80+
static _HiddenComponentScope? maybeOf(BuildContext context) =>
81+
context.dependOnInheritedWidgetOfExactType<_HiddenComponentScope>();
82+
}
83+
5484
/// Abstract representation of a document layout.
5585
///
5686
/// Regardless of how a document is displayed, a [DocumentLayout] needs
@@ -319,7 +349,7 @@ mixin DocumentComponent<T extends StatefulWidget> on State<T> {
319349

320350
/// Returns `true` if the component is visually hidden and should be ignored
321351
/// for the purposes of hit testing, cursor movement, etc.
322-
bool get isHidden => context.findAncestorWidgetOfExactType<HiddenComponent>()?.hiding ?? false;
352+
bool get isHidden => context.dependOnInheritedWidgetOfExactType<_HiddenComponentScope>()?.hiding ?? false;
323353

324354
/// Returns the desired [MouseCursor] at the given (x,y) [localOffset], or
325355
/// [null] if this component has no preference for the cursor style.

0 commit comments

Comments
 (0)