Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Gameface's cohinline attribute in dom-fiber #124

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Copy link
Contributor

@ja-ni ja-ni Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice comment highlighting the experimental feature 👍🏼

}

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
Loading