Skip to content

Commit 17c70fc

Browse files
committed
fix(graphql-playground-react): side tabs doesn't appear
This commit fixes the following bug: side tabs doesn't appear when GraphQLEditor component isn't updated
1 parent 857264d commit 17c70fc

File tree

4 files changed

+78
-56
lines changed

4 files changed

+78
-56
lines changed

packages/graphql-playground-react/src/components/Playground/EditorWrapper.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -904,12 +904,24 @@ const GraphqlContainer = styled.div`
904904
width: 100%;
905905
`
906906

907-
interface Props {
908-
setRef?: (ref: any) => void
909-
}
907+
export class Container extends React.PureComponent {
908+
private graphqlContainer
909+
910+
render() {
911+
return (
912+
<GraphqlContainer ref={this.setGraphqlContainer}>
913+
{this.props.children}
914+
</GraphqlContainer>
915+
)
916+
}
910917

911-
export const Container: React.SFC<Props> = ({ children, setRef }) => (
912-
<GraphqlContainer ref={setRef}>{children}</GraphqlContainer>
913-
)
918+
getWidth = () => {
919+
return this.graphqlContainer.offsetWidth
920+
}
921+
922+
private setGraphqlContainer = ref => {
923+
this.graphqlContainer = ref
924+
}
925+
}
914926

915927
export default Wrapper

packages/graphql-playground-react/src/components/Playground/ExplorerTabs/SideTabs.tsx

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { getLeft } from 'graphiql/dist/utility/elementPosition'
66
import {
77
addStack,
88
toggleDocs,
9-
changeWidthDocs,
109
changeKeyMove,
1110
setDocsVisible,
1211
} from '../../../state/docs/actions'
@@ -40,12 +39,14 @@ export interface Props {
4039
sessionId: string
4140
children: Array<React.ReactElement<any>>
4241
maxWidth: number
42+
setWidth: (props?: any) => any
43+
setActiveContentRef: (ref: any) => void
4344
}
4445

4546
export interface SideTabContentProps {
4647
schema: GraphQLSchema
4748
sessionId: string
48-
setWidth: (props: any) => any
49+
setWidth: (props?: any) => any
4950
}
5051

5152
export interface State {
@@ -58,7 +59,6 @@ class SideTabs extends React.Component<
5859
State
5960
> {
6061
ref
61-
public activeContentComponent: any // later React.Component<...>
6262
private refContentContainer: any
6363
private clientX: number = 0
6464
private clientY: number = 0
@@ -67,27 +67,6 @@ class SideTabs extends React.Component<
6767
;(window as any).d = this
6868
}
6969

70-
setWidth = (props: any = this.props) => {
71-
if (!this.activeContentComponent) {
72-
return
73-
}
74-
if (!this.props.docs.docsOpen) {
75-
return
76-
}
77-
requestAnimationFrame(() => {
78-
const width = this.activeContentComponent.getWidth(props)
79-
this.props.changeWidthDocs(
80-
props.sessionId,
81-
Math.min(width, this.props.maxWidth),
82-
)
83-
})
84-
}
85-
setActiveContentRef = ref => {
86-
if (ref) {
87-
this.activeContentComponent = ref.getWrappedInstance()
88-
}
89-
}
90-
9170
componentDidUpdate(prevProps) {
9271
if (!prevProps.docs.activeTabIdx && this.props.docs.activeTabIdx) {
9372
this.props.setDocsVisible(
@@ -99,7 +78,7 @@ class SideTabs extends React.Component<
9978
if (prevProps.activeTabIdx && !this.props.docs.activeTabIdx) {
10079
this.props.setDocsVisible(this.props.sessionId, false)
10180
}
102-
this.setWidth()
81+
this.props.setWidth()
10382
if (
10483
this.props.docs.activeTabIdx !== prevProps.docs.activeTabIdx &&
10584
this.refContentContainer
@@ -112,7 +91,7 @@ class SideTabs extends React.Component<
11291
if (!this.props.docs.activeTabIdx) {
11392
this.props.setDocsVisible(this.props.sessionId, false)
11493
}
115-
return this.setWidth()
94+
return this.props.setWidth()
11695
}
11796

11897
render() {
@@ -149,8 +128,8 @@ class SideTabs extends React.Component<
149128
{activeTab &&
150129
React.cloneElement(activeTab.props.children, {
151130
...activeTab.props,
152-
ref: this.setActiveContentRef,
153-
setWidth: this.setWidth,
131+
ref: this.props.setActiveContentRef,
132+
setWidth: this.props.setWidth,
154133
})}
155134
</TabContentContainer>
156135
</Tabs>
@@ -168,7 +147,7 @@ class SideTabs extends React.Component<
168147
private handleTabClick = idx => () => {
169148
if (this.props.docs.activeTabIdx === idx) {
170149
this.props.setDocsVisible(this.props.sessionId, false)
171-
return this.setWidth()
150+
return this.props.setWidth()
172151
}
173152
if (this.props.docs.activeTabIdx !== idx) {
174153
this.props.setDocsVisible(
@@ -177,10 +156,10 @@ class SideTabs extends React.Component<
177156
this.props.docs.activeTabIdx,
178157
)
179158
this.props.setDocsVisible(this.props.sessionId, true, idx)
180-
return this.setWidth()
159+
return this.props.setWidth()
181160
} else {
182161
this.props.setDocsVisible(this.props.sessionId, true, idx)
183-
return this.setWidth()
162+
return this.props.setWidth()
184163
}
185164
}
186165

@@ -270,7 +249,6 @@ const mapDispatchToProps = dispatch =>
270249
{
271250
addStack,
272251
toggleDocs,
273-
changeWidthDocs,
274252
changeKeyMove,
275253
setDocsVisible,
276254
},

packages/graphql-playground-react/src/components/Playground/GraphQLEditor.tsx

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ import {
6666
fetchSchema,
6767
} from '../../state/sessions/actions'
6868
import { ResponseRecord } from '../../state/sessions/reducers'
69+
import { getDocsOpen } from '../../state/docs/selectors'
70+
import { changeWidthDocs } from '../../state/docs/actions'
6971

7072
/**
7173
* The top-level React component for GraphQLEditor, intended to encompass the entire
@@ -94,7 +96,9 @@ export interface ReduxProps {
9496
toggleVariables: () => void
9597
setEditorFlex: (flex: number) => void
9698
stopQuery: (sessionId: string) => void
99+
changeWidthDocs: (sessionId: string, width: number) => void
97100
navStack: any[]
101+
docsOpen: boolean
98102
// sesion props
99103
queryRunning: boolean
100104
responses: List<ResponseRecord>
@@ -140,6 +144,7 @@ class GraphQLEditor extends React.PureComponent<Props & ReduxProps> {
140144
private queryVariablesRef
141145
private httpHeadersRef
142146
private containerComponent
147+
private activeSideTabContent
143148

144149
componentDidMount() {
145150
// Ensure a form of a schema exists (including `null`) and
@@ -167,7 +172,7 @@ class GraphQLEditor extends React.PureComponent<Props & ReduxProps> {
167172

168173
render() {
169174
return (
170-
<Container setRef={this.setContainerComponent}>
175+
<Container ref={this.setContainerComponent}>
171176
<EditorWrapper>
172177
<TopBar
173178
shareEnabled={this.props.shareEnabled}
@@ -265,23 +270,25 @@ class GraphQLEditor extends React.PureComponent<Props & ReduxProps> {
265270
</ResultWrap>
266271
</EditorBar>
267272
</EditorWrapper>
268-
{this.containerComponent && (
269-
<SideTabs maxWidth={this.containerComponent.offsetWidth - 86}>
270-
<SideTab label="Docs" activeColor="green" tabWidth="49px">
271-
<GraphDocs
272-
schema={this.props.schema}
273-
ref={this.setDocExplorerRef}
274-
/>
275-
</SideTab>
276-
<SideTab label="Schema" activeColor="blue" tabWidth="65px">
277-
<SDLView
278-
schema={this.props.schema}
279-
ref={this.setSchemaExplorerRef}
280-
sessionId={this.props.sessionId}
281-
/>
282-
</SideTab>
283-
</SideTabs>
284-
)}
273+
<SideTabs
274+
setActiveContentRef={this.setSideTabActiveContentRef}
275+
setWidth={this.setDocsWidth}
276+
>
277+
<SideTab label="Docs" activeColor="green" tabWidth="49px">
278+
<GraphDocs
279+
schema={this.props.schema}
280+
ref={this.setDocExplorerRef}
281+
/>
282+
</SideTab>
283+
<SideTab label="Schema" activeColor="blue" tabWidth="65px">
284+
<SDLView
285+
schema={this.props.schema}
286+
ref={this.setSchemaExplorerRef}
287+
sessionId={this.props.sessionId}
288+
/>
289+
</SideTab>
290+
</SideTabs>
291+
}
285292
</Container>
286293
)
287294
}
@@ -343,6 +350,12 @@ class GraphQLEditor extends React.PureComponent<Props & ReduxProps> {
343350
}
344351
}
345352

353+
setSideTabActiveContentRef = ref => {
354+
if (ref) {
355+
this.activeSideTabContent = ref.getWrappedInstance()
356+
}
357+
}
358+
346359
/**
347360
* Inspect the query, automatically filling in selection sets for non-leaf
348361
* fields which do not yet have them.
@@ -565,6 +578,20 @@ class GraphQLEditor extends React.PureComponent<Props & ReduxProps> {
565578
}
566579
}
567580
}
581+
582+
private setDocsWidth = (props: any = this.props) => {
583+
if (!this.activeSideTabContent) {
584+
return
585+
}
586+
if (!this.props.docsOpen) {
587+
return
588+
}
589+
requestAnimationFrame(() => {
590+
const width = this.activeSideTabContent.getWidth()
591+
const maxWidth = this.containerComponent.getWidth() - 86
592+
this.props.changeWidthDocs(props.sessionId, Math.min(width, maxWidth))
593+
})
594+
}
568595
}
569596

570597
const mapStateToProps = createStructuredSelector({
@@ -586,6 +613,7 @@ const mapStateToProps = createStructuredSelector({
586613
operationName: getOperationName,
587614
headersCount: getHeadersCount,
588615
sessionId: getSelectedSessionIdFromRoot,
616+
docsOpen: getDocsOpen,
589617
})
590618

591619
export default // TODO fix redux types
@@ -605,6 +633,7 @@ connect<any, any, any>(
605633
setEditorFlex,
606634
toggleVariables,
607635
fetchSchema,
636+
changeWidthDocs,
608637
},
609638
null,
610639
{

packages/graphql-playground-react/src/state/docs/selectors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ export const getSessionDocsState = createSelector(
1313
export const getSessionDocs = createSelector([getSessionDocsState], state => {
1414
return state.toJS()
1515
})
16+
export const getDocsOpen = createSelector([getSessionDocsState], state => {
17+
return state.get('docsOpen')
18+
})

0 commit comments

Comments
 (0)