You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.8 KiB

5 years ago
  1. // This interface is only exposed and any Riot component will receive the following properties
  2. export interface RiotCoreComponent<P = object, S = object> {
  3. // automatically generated on any component instance
  4. readonly props: P
  5. readonly root: HTMLElement
  6. readonly name?: string
  7. // TODO: add the @riotjs/dom-bindings types
  8. readonly slots: any[]
  9. mount(
  10. element: HTMLElement,
  11. initialState?: S,
  12. parentScope?: object
  13. ): RiotComponent<P, S>
  14. update(
  15. newState?: Partial<S>,
  16. parentScope?: object
  17. ): RiotComponent<P, S>
  18. unmount(keepRootElement: boolean): RiotComponent<P, S>
  19. // Helpers
  20. $(selector: string): HTMLElement
  21. $$(selector: string): [HTMLElement]
  22. }
  23. // This object interface is created anytime a riot file will be compiled into javascript
  24. export interface RiotComponentShell<P = object, S = object> {
  25. readonly css?: string
  26. readonly exports?: () => RiotComponentExport<P, S>|object
  27. readonly name?: string
  28. // TODO: add the @riotjs/dom-bindings types
  29. template(): any
  30. }
  31. // Interface that can be used when creating the components export
  32. export interface RiotComponentExport<P = object, S = object> {
  33. // optional on the component object
  34. state?: S
  35. // optional alias to map the children component names
  36. components?: {
  37. [key: string]: RiotComponentShell<P, S>
  38. }
  39. // state handling methods
  40. shouldUpdate?(newProps: P, currentProps: P): boolean
  41. // lifecycle methods
  42. onBeforeMount?(currentProps: P, currentState: S): void
  43. onMounted?(currentProps: P, currentState: S): void
  44. onBeforeUpdate?(currentProps: P, currentState: S): void
  45. onUpdated?(currentProps: P, currentState: S): void
  46. onBeforeUnmount?(currentProps: P, currentState: S): void
  47. onUnmounted?(currentProps: P, currentState: S): void
  48. [key: string]: any
  49. }
  50. // All the RiotComponent Public interface properties are optional
  51. export interface RiotComponent<P = object, S = object> extends RiotCoreComponent<P, S>, RiotComponentExport<P, S> {}
  52. export type RegisteredComponentsMap = Map<string, () => RiotComponent>
  53. export type ComponentEnhancer = <P, S>(component: RiotComponent<P, S>) => RiotComponent<P, S>
  54. export type InstalledPluginsSet = Set<ComponentEnhancer>
  55. export function register<P, S>(componentName: string, shell: RiotComponentShell<P, S>): RegisteredComponentsMap
  56. export function unregister(componentName: string): RegisteredComponentsMap
  57. export function mount<P = object, S = object>(selector: string, initialProps?: P, componentName?: string): RiotComponent<P, S>[]
  58. export function unmount(selector: string, keepRootElement: boolean):HTMLElement[]
  59. export function install(plugin: ComponentEnhancer):InstalledPluginsSet
  60. export function uninstall(plugin: ComponentEnhancer):InstalledPluginsSet
  61. export function component<P , S>(shell: RiotComponentShell<P, S>):(el: HTMLElement, initialProps?: P) => RiotComponent<P, S>
  62. export const version: string