Is it ok to take magnesium while pregnant or breastfeeding?

Yes. Yes magnesium is a mineral that's important for many cell functions and also important in bone formation.
If you need it! No reason to take magnesium unless you are found to have low blood levels of it i.e. Deficient in it.