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

Expose TextAncestorContext #2447

Closed
1 task done
flip-it opened this issue Dec 5, 2022 · 2 comments
Closed
1 task done

Expose TextAncestorContext #2447

flip-it opened this issue Dec 5, 2022 · 2 comments
Labels
enhancement Requires extension or creation of new React Native API

Comments

@flip-it
Copy link
Contributor

flip-it commented Dec 5, 2022

Is there an existing request?

  • I have searched for this request

Describe the feature request

The Text component uses the context TextAncestorContext to determine if it is an ancestor of another Text component. It controls what element and style to be rendered.

It would be beneficial to hook in to the same context in the application code. For example when building text components with custom styling using RNW’s Text component, it would make sense to apply some styles only on the parent and let the ancestor inherit.

In the following example the blue text doesn’t get large (because of the default style).

function MyText({ large, blue, children }) {
  return (
    <Text style={[styles.defaults, large && styles.large, blue && styles.blue]}>
      {children}
    </Text>
  );
}

const styles = StyleSheet.create({
  defaults: {
    fontSize: 16,
    fontFamily: "Times"
  },
  large: {
    fontSize: 21
  },
  blue: {
    color: "blue"
  }
});

<MyText large>
  Go to
  <MyText blue href="/page">
    a page
  </MyText>
</MyText>

I imagine using the TextAncestorContext like this:

function MyText({ large, blue, children }) {
  const hasTextAncestor = React.useContext(TextAncestorContext);

  return (
    <Text style={[!hasTextAncestor && styles.defaults, large && styles.large, blue && styles.blue]}>
      {children}
    </Text>
  );
}

CodeSandbox

@flip-it flip-it added the enhancement Requires extension or creation of new React Native API label Dec 5, 2022
@necolas
Copy link
Owner

necolas commented Dec 13, 2022

@necolas
Copy link
Owner

necolas commented Apr 13, 2023

I'm going to close this because a) React Native needs to make this change too, b) you can create your own Text context to workaround the issues for now.

@necolas necolas closed this as completed Apr 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Requires extension or creation of new React Native API
Projects
None yet
Development

No branches or pull requests

2 participants