Skip to content

Commit

Permalink
Support for Gameface's cohinline attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
pirelenito committed Aug 1, 2023
1 parent 2c1eeab commit a5d02fb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"cSpell.words": [
"cohinline",
"Gameface",
"gamepad"
],
"typescript.tsdk": "./node_modules/typescript/lib"
Expand Down
36 changes: 36 additions & 0 deletions packages/@react-facet/dom-fiber/src/setupHostConfig.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ describe('mount', () => {
expect(root?.innerHTML ?? '').toBe('<input disabled="">')
})

it('sets cohinline', () => {
render(<fast-p cohinline />)
expect(root?.innerHTML ?? '').toBe('<p cohinline=""></p>')
})

it('sets maxlength', () => {
render(<input type="text" maxLength={10} />)
expect(root?.innerHTML ?? '').toBe('<input maxlength="10" type="text">')
Expand Down Expand Up @@ -370,6 +375,16 @@ describe('mount', () => {
expect(root?.innerHTML ?? '').toBe('<input>')
})

it('sets cohinline', () => {
const cohinlineFacet = createFacet({ initialValue: true })

render(<fast-p cohinline={cohinlineFacet} />)
expect(root?.innerHTML ?? '').toBe('<p cohinline=""></p>')

cohinlineFacet.set(false)
expect(root?.innerHTML ?? '').toBe('<p></p>')
})

it('sets maxlength', () => {
const maxLengthFacet = createFacet({ initialValue: 10 })

Expand Down Expand Up @@ -1134,6 +1149,27 @@ describe('update', () => {
expect(root?.innerHTML ?? '').toBe('<input>')
})

it('updates cohinline', () => {
const MockComponent = () => {
const [cohinline, setCohinline] = useState<boolean | undefined>(true)
useEffect(() => {
setTimeout(() => setCohinline(false), 1)
setTimeout(() => setCohinline(true), 2)
setTimeout(() => setCohinline(undefined), 3)
}, [])
return <fast-p cohinline={cohinline} />
}

render(<MockComponent />)
expect(root?.innerHTML ?? '').toBe('<p cohinline=""></p>')
jest.advanceTimersByTime(1)
expect(root?.innerHTML ?? '').toBe('<p></p>')
jest.advanceTimersByTime(1)
expect(root?.innerHTML ?? '').toBe('<p cohinline=""></p>')
jest.advanceTimersByTime(1)
expect(root?.innerHTML ?? '').toBe('<p></p>')
})

it('updates maxLength', () => {
const MockComponent = () => {
const [maxLength, setMaxLength] = useState<number | undefined>(1)
Expand Down
13 changes: 13 additions & 0 deletions packages/@react-facet/dom-fiber/src/setupHostConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ export const setupHostConfig = (): HostConfig<
newProps['data-x-ray'] != null
? setupAttributeUpdate('data-x-ray', newProps['data-x-ray'], element)
: undefined,

cohinline:
newProps.cohinline != null ? setupAttributeUpdate('cohinline', newProps.cohinline, element) : undefined,
}
},

Expand Down Expand Up @@ -881,6 +884,16 @@ export const setupHostConfig = (): HostConfig<
}
}

if (newProps.cohinline !== oldProps.cohinline) {
instance.cohinline?.()

if (newProps.cohinline == null) {
element.removeAttribute('cohinline')
} else {
instance.cohinline = setupAttributeUpdate('cohinline', newProps.cohinline, element)
}
}

if (newProps.onClick !== oldProps.onClick) {
if (oldProps.onClick) element.removeEventListener('click', oldProps.onClick)
if (newProps.onClick) element.addEventListener('click', newProps.onClick)
Expand Down
7 changes: 7 additions & 0 deletions packages/@react-facet/dom-fiber/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ export type ElementProps<T> = PointerEvents &
stopOpacity?: FacetProp<string | undefined>
fontFamily?: FacetProp<string | undefined>
fontSize?: FacetProp<string | undefined>

/**
* Support for Gameface's Experimental inline layout for paragraph elements
* More info: https://docs.coherent-labs.com/cpp-gameface/what_is_gfp/htmlfeaturesupport/#experimental-inline-layout-for-paragraph-elements
*/
cohinline?: FacetProp<boolean | undefined>
}

export type TextProps = {
Expand Down Expand Up @@ -242,6 +248,7 @@ export type ElementContainer = {
stopOpacity?: Unsubscribe
fontFamily?: Unsubscribe
fontSize?: Unsubscribe
cohinline?: Unsubscribe
}

export const isElementContainer = (value: ElementContainer | TextContainer): value is ElementContainer => {
Expand Down

0 comments on commit a5d02fb

Please sign in to comment.