@ -2,50 +2,6 @@ import { Path } from 'leaflet';
import type { PropDef , PropTypesFromTable } from './props.ts' ;
import type { LatLngBoundsExpression } from 'leaflet' ;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Ctor < T = object > = new ( . . . args : any [ ] ) = > T ;
export function WithProps < TBase extends Ctor < HTMLElement > , TProps extends Record < string , PropDef > > (
Base : TBase ,
props : TProps ,
) : TBase & Ctor < HTMLElement & PropTypesFromTable < TProps > > {
class WithProps extends Base {
static {
definePropAccessors ( WithProps . prototype , props ) ;
}
static get observedAttributes ( ) : string [ ] {
return Object . values ( props ) . map ( ( s ) = > s . attr ) ;
}
}
return WithProps as TBase & Ctor < HTMLElement & PropTypesFromTable < TProps > > ;
}
// Installs reactive getter/setter pairs on a prototype for every entry in
// a PROPS table. Each getter reads from the attribute (coerced to the
// correct type), each setter writes via setAttribute/toggleAttribute.
// Used by the WithProps() mixin so components have `el.lat = 51.5` sugar.
function definePropAccessors ( proto : object , props : Record < string , PropDef > ) {
for ( const [ name , spec ] of Object . entries ( props ) ) {
Object . defineProperty ( proto , name , {
get ( ) {
const el = this as HTMLElement ;
const val = el . getAttribute ( spec . attr ) ;
if ( spec . kind === 'num' ) return val !== null ? + val : spec.default ;
if ( spec . kind === 'bool-on' ) return el . hasAttribute ( spec . attr ) ;
return val ? ? ( spec as { default : string } ) . default ;
} ,
set ( v : unknown ) {
const el = this as HTMLElement ;
if ( spec . kind === 'bool-on' ) el . toggleAttribute ( spec . attr , ! ! v ) ;
else el . setAttribute ( spec . attr , ` ${ v } ` ) ;
} ,
configurable : true ,
enumerable : true ,
} ) ;
}
}
// Reads a numeric attribute from an element, falling back to the default
// value declared in the PROPS table. Handles the common pattern of
// reading lat/lng/radius/opacity with a guaranteed number return.