Skip to content

Commit 39eb8d5

Browse files
committed
wip
1 parent c26311f commit 39eb8d5

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

packages/signals/signals-runtime/src/web/web-signals-types.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,50 @@ export interface RawSignal<T extends SignalTypes, Data> extends BaseSignal {
77
data: Data
88
metadata?: Record<string, any>
99
}
10-
1110
export type InteractionData = ClickData | SubmitData | ChangeData
1211

13-
interface SerializedTarget {
12+
export type ParsedAttributes = { [attributeName: string]: string | null }
13+
14+
export interface TargetedHTMLElement {
15+
id: string
16+
attributes: ParsedAttributes
1417
[key: string]: any
1518
}
1619

1720
type ClickData = {
1821
eventType: 'click'
19-
target: SerializedTarget
22+
target: TargetedHTMLElement
2023
}
2124

2225
type SubmitData = {
2326
eventType: 'submit'
24-
submitter?: SerializedTarget
25-
target: SerializedTarget
27+
submitter?: TargetedHTMLElement
28+
target: TargetedHTMLElement
2629
}
2730

2831
export type ChangeData = {
2932
eventType: 'change'
30-
target: SerializedTarget
33+
/**
34+
* The target element that changed.
35+
*/
36+
target: TargetedHTMLElement
37+
/**
38+
* The name/type of "listener" that triggered the change.
39+
* Elements can change due to a variety of reasons, such as a mutation, a change event, or a contenteditable change
40+
*/
3141
listener: 'contenteditable' | 'onchange' | 'mutation'
42+
/**
43+
* The change that occurred -- this is a key-value object of the change that occurred
44+
* For mutation listeners, this is the attributes that changed
45+
* For contenteditable listeners, this is the text that changed
46+
* @example
47+
* ```ts
48+
* { checked: true } // onchange
49+
* { value: 'new value' } // onchange / mutation
50+
* {'aria-selected': 'true' } // mutation
51+
* { textContent: 'Sentence1\nSentence2\n' } // contenteditable
52+
* ```
53+
*/
3254
change: JSONValue
3355
}
3456

packages/signals/signals/src/core/signal-generators/dom-gen/dom-gen.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import {
66
import { SignalEmitter } from '../../emitter'
77
import { SignalGenerator } from '../types'
88
import { cleanText } from './helpers'
9+
import type {
10+
ParsedAttributes,
11+
TargetedHTMLElement,
12+
} from '@segment/analytics-signals-runtime'
913

1014
interface Label {
1115
textContent: string
@@ -38,11 +42,11 @@ const parseToLabel = (label: HTMLElement): Label => {
3842
}
3943
}
4044

41-
const parseNodeMap = (nodeMap: NamedNodeMap): Record<string, unknown> => {
42-
return Array.from(nodeMap).reduce((acc, attr) => {
45+
const parseNodeMap = (nodeMap: NamedNodeMap): ParsedAttributes => {
46+
return Array.from(nodeMap).reduce<ParsedAttributes>((acc, attr) => {
4347
acc[attr.name] = attr.value
4448
return acc
45-
}, {} as Record<string, unknown>)
49+
}, {})
4650
}
4751

4852
interface ParsedElementBase {

0 commit comments

Comments
 (0)