Skip to content
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 src/figure-caption.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ const FigureCaption = ({ as = 'figcaption', number, children }) => {
)
}

FigureCaption.displayName = 'FigureCaption'

export default FigureCaption
17 changes: 17 additions & 0 deletions src/figure.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,28 @@ import { Box } from 'theme-ui'
import Group from './group'

const Figure = ({ as = 'figure', children, sx }) => {
// try to use figure/table number as id for anchoring
const childrenArray = React.Children.toArray(children)
const captionElement = childrenArray.find(
(child) =>
React.isValidElement(child) &&
child.type?.displayName &&
(child.type.displayName === 'FigureCaption' ||
child.type.displayName === 'TableCaption')
)

const elementNumber = captionElement?.props?.number
const elementType =
captionElement?.type?.displayName === 'TableCaption' ? 'table' : 'figure'
Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible to infer the elementType by inspecting the label prop passed to Caption? That way, we could handle other caption labels automatically if they ever arose. Or is inspecting a grandchild's props tricky?

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately, I haven't found a clean way to access Caption's props from Figure. We could potentially remove the FigureCaption and TableCaption components and just use Caption with a required label prop, but that's a bigger change than I want to do here.

const id = elementNumber ? `${elementType}-${elementNumber}` : undefined

return (
<Box
as={as}
id={id}
sx={{
my: [6, 6, 6, 7],
scrollMarginTop: '60px', // account for header height
'@media print': {
breakInside: 'avoid',
},
Expand Down
2 changes: 2 additions & 0 deletions src/table-caption.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ const TableCaption = ({ as = 'figcaption', number, children }) => {
)
}

TableCaption.displayName = 'TableCaption'

export default TableCaption
Loading