Skip to content
Open
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
4 changes: 4 additions & 0 deletions packages/ui-alerts/src/Alert/v1/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ class Alert extends Component<AlertProps, AlertState> {

handleRef = (el: Element | null) => {
this.ref = el
const { elementRef } = this.props
if (typeof elementRef === 'function') {
elementRef(el)
}
}

handleTimeout = () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/ui-alerts/src/Alert/v1/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ type AlertOwnProps = {
* An icon, or function that returns an icon. Setting it will override the variant's icon.
*/
renderCustomIcon?: Renderable

/**
* provides a reference to the underlying html root element
*/
elementRef?: (element: Element | null) => void
}

type PropKeys = keyof AlertOwnProps
Expand Down Expand Up @@ -137,7 +142,8 @@ const allowedProps: AllowedPropKeys = [
'transition',
'open',
'hasShadow',
'renderCustomIcon'
'renderCustomIcon',
'elementRef'
]

type AlertState = {
Expand Down
4 changes: 4 additions & 0 deletions packages/ui-alerts/src/Alert/v2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ class Alert extends Component<AlertProps, AlertState> {

handleRef = (el: Element | null) => {
this.ref = el
const { elementRef } = this.props
if (typeof elementRef === 'function') {
elementRef(el)
}
}

handleTimeout = () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/ui-alerts/src/Alert/v2/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ type AlertOwnProps = {
* An icon, or function that returns an icon. Setting it will override the variant's icon.
*/
renderCustomIcon?: Renderable

/**
* provides a reference to the underlying html root element
*/
elementRef?: (element: Element | null) => void
}

type PropKeys = keyof AlertOwnProps
Expand Down Expand Up @@ -138,7 +143,8 @@ const allowedProps: AllowedPropKeys = [
'transition',
'open',
'hasShadow',
'renderCustomIcon'
'renderCustomIcon',
'elementRef'
]

type AlertState = {
Expand Down
51 changes: 38 additions & 13 deletions packages/ui-motion/src/Transition/BaseTransition/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
* SOFTWARE.
*/

import { Component, ReactElement } from 'react'
import { Component, ReactElement, ReactInstance } from 'react'

import { getClassList, findDOMNode } from '@instructure/ui-dom-utils'
import {
ensureSingleChild,
safeCloneElement
} from '@instructure/ui-react-utils'
import { createChainedFunction } from '@instructure/ui-utils'

import { allowedProps } from './props.js'
import type {
Expand Down Expand Up @@ -334,19 +335,43 @@ class BaseTransition extends Component<
}

renderChildren() {
return this.props.children
? safeCloneElement(
ensureSingleChild(this.props.children) as ReactElement,
{
'aria-hidden': !this.props.in ? true : undefined,
ref: (el: React.ReactInstance | null) => {
const ref = (findDOMNode(el) as Element) || null

this.handleRef(ref)
}
if (!this.props.children) {
return null
}

const child = ensureSingleChild(this.props.children) as ReactElement

const elementOnlyRef = (el: ReactInstance | Element | null) => {
if (el instanceof Element) {
this.handleRef(el)
}
}

// `typeof type === 'object'` => forwardRef wrapper (withStyle-decorated InstUI components)
const refProps =
typeof child.type === 'object'
? {
// chain so the child's own elementRef still fires instead of being overwritten
elementRef: createChainedFunction(
(child.props as { elementRef?: (el: Element | null) => void })
?.elementRef,
this.handleRef
),
// fallback for forwardRef children that expose their node via `ref`, not elementRef
ref: elementOnlyRef
}
)
: null
: {
// for host el / plain class|fn: findDOMNode is the fallback
ref: (el: ReactInstance | Element | null) =>
this.handleRef(
el instanceof Element ? el : (findDOMNode(el) as Element) ?? null
)
}

return safeCloneElement(child, {
'aria-hidden': !this.props.in ? true : undefined,
...refProps
})
}

render() {
Expand Down
Loading