Add setDefaultValue to StyleProp (#4044)

This PR adds a way to set the default value of a style property.

### Change type

- [ ] `bugfix`
- [ ] `improvement`
- [x] `feature`
- [ ] `api`
- [ ] `other`

### Release notes

- Adds a method for changing the default style of a `StyleProp`
instance.
This commit is contained in:
Steve Ruiz 2024-07-01 11:21:33 +01:00 committed by GitHub
parent 4fff8a89af
commit cafa0f5636
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 33 additions and 2 deletions

View file

@ -0,0 +1,12 @@
import { DefaultSizeStyle, Tldraw } from 'tldraw'
import 'tldraw/tldraw.css'
DefaultSizeStyle.setDefaultValue('s')
export default function ChangingDefaultStyleExample() {
return (
<div className="tldraw__editor">
<Tldraw persistenceKey="example" />
</div>
)
}

View file

@ -0,0 +1,13 @@
---
title: Changing default style
component: ./ChangingDefaultStyleExample.tsx
category: ui
priority: 1
keywords: [size, styles, default]
---
Change the default value for a style prop.
---
Want to set the default value for a property to something other than it's built-in default? In this example we make the size style have small as its default calue.

View file

@ -839,7 +839,7 @@ export class StyleProp<Type> implements T.Validatable<Type> {
// @internal
protected constructor(id: string, defaultValue: Type, type: T.Validatable<Type>);
// (undocumented)
readonly defaultValue: Type;
defaultValue: Type;
static define<Type>(uniqueId: string, options: {
defaultValue: Type;
type?: T.Validatable<Type>;
@ -851,6 +851,8 @@ export class StyleProp<Type> implements T.Validatable<Type> {
// (undocumented)
readonly id: string;
// (undocumented)
setDefaultValue(value: Type): void;
// (undocumented)
readonly type: T.Validatable<Type>;
// (undocumented)
validate(value: unknown): Type;

View file

@ -83,10 +83,14 @@ export class StyleProp<Type> implements T.Validatable<Type> {
/** @internal */
protected constructor(
readonly id: string,
readonly defaultValue: Type,
public defaultValue: Type,
readonly type: T.Validatable<Type>
) {}
setDefaultValue(value: Type) {
this.defaultValue = value
}
validate(value: unknown) {
return this.type.validate(value)
}