Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add colorOrNull knob #1027

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/knobs/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@ Widgetbook's UI:
| int.input | `int` |
| intOrNull.input | `int?` |
| color | `Color` |
| colorOrNull | `Color?` |
5 changes: 3 additions & 2 deletions examples/knobs_example/lib/widgetbook.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ class WidgetbookApp extends StatelessWidget {
label: 'color',
initialValue: const Color(0xFF000000),
),
nullable: null, // N/A
nullable: context.knobs.colorOrNull(
label: 'colorOrNull',
), // N/A
),
KnobEntry<Duration>(
name: 'Duration',
Expand All @@ -123,7 +125,6 @@ class WidgetbookApp extends StatelessWidget {
),
nullable: context.knobs.dateTimeOrNull(
label: 'dateTimeOrNull',
initialValue: initialDate,
start: DateTime(initialDate.year - 1),
end: DateTime(initialDate.year + 1),
),
Expand Down
1 change: 1 addition & 0 deletions packages/widgetbook/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- **FEAT**: Add light theme support. ([#919](https://github.com/widgetbook/widgetbook/pull/919))
- **FIX**: Maintain navigation panel state on reload. ([#932](https://github.com/widgetbook/widgetbook/pull/932))
- **FEAT**: Add spaces to `color` knob. ([#986](https://github.com/widgetbook/widgetbook/pull/986))
- **FEAT**: Add `colorOrNull` knob. ([#1027](https://github.com/widgetbook/widgetbook/pull/1027))
- **FEAT**: Add `duration` knob. ([#934](https://github.com/widgetbook/widgetbook/pull/934))
- **FEAT**: Add `dateTime` knob. ([#940](https://github.com/widgetbook/widgetbook/pull/940))
- **FEAT**: Add `int` knob. ([#942](https://github.com/widgetbook/widgetbook/pull/942))
Expand Down
20 changes: 18 additions & 2 deletions packages/widgetbook/lib/src/knobs/builders/knobs_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ class KnobsBuilder {
);
}

/// Creates a textfield that can be typed in and optionally hold a
/// color value
/// Creates a color picker that can be used to select a color.
Color color({
required String label,
required Color initialValue,
Expand All @@ -77,6 +76,23 @@ class KnobsBuilder {
)!;
}

/// Creates a color picker that can be used to select a color.
Color? colorOrNull({
required String label,
Color? initialValue,
ColorSpace initialColorSpace = ColorSpace.hex,
String? description,
}) {
return onKnobAdded(
ColorKnob.nullable(
label: label,
initialValue: initialValue,
initialColorSpace: initialColorSpace,
description: description,
),
);
}

/// Creates a textfield that can be typed in
String string({
required String label,
Expand Down
15 changes: 11 additions & 4 deletions packages/widgetbook/lib/src/knobs/color_knob.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@ import '../fields/fields.dart';
import 'knob.dart';

@internal
class ColorKnob extends Knob<Color> {
class ColorKnob extends Knob<Color?> {
ColorKnob({
required super.label,
required super.initialValue,
super.description,
this.initialColorSpace = ColorSpace.hex,
});

ColorSpace initialColorSpace;
ColorKnob.nullable({
required super.label,
required super.initialValue,
super.description,
this.initialColorSpace = ColorSpace.hex,
}) : super(isNullable: true);

final ColorSpace initialColorSpace;

@override
List<Field> get fields {
Expand All @@ -28,7 +35,7 @@ class ColorKnob extends Knob<Color> {
}

@override
Color valueFromQueryGroup(Map<String, String> group) {
return valueOf(label, group)!;
Color? valueFromQueryGroup(Map<String, String> group) {
return valueOf(label, group);
}
}